Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > is this declaration correct

Reply
Thread Tools

is this declaration correct

 
 
Raj Pashwar
Guest
Posts: n/a
 
      07-18-2012
Rather than
include <stdio.h> can I use int printf(const char *,...) correct at all
places if I only use printf in my program..

Cheers

---
"You are not going to get it right first time" -> Bjarne Stroustrup
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      07-18-2012
Raj Pashwar <(E-Mail Removed)> writes:
> Rather than
> include <stdio.h> can I use int printf(const char *,...) correct at all
> places if I only use printf in my program..


You *can*, but there's no good reason to do so.

It's permitted by 7.1.4p2 (7.1.7 in C89/C90):

Provided that a library function can be declared without
reference to any type defined in a header, it is also permissible
to declare the function and use it without including its
associated header.

The <stdio.h> header exists precisely so you don't have write your own
declarations for standard functions. Using it guarantees (assuming your
implementation isn't broken) that the declarations will be correct.
If you write your own declarations, there's always a risk that you'll
get something wrong.

And in fact, you have. The declaration you show:

int printf(const char *,...);

is correct for C89/C90, but starting with C99 the declaration changed
to:

int printf(const char * restrict, ...);

(Omitting the "restrict" *probably* won't cause any problems, but it
could inhibit some optimizations.)

And even that works only because printf doesn't happen to depend on any
other declarations in <stdio.h>. You can't declare fprintf this way, for
example, because it depends on the definition of FILE -- and its
definition can vary substantially from one implementation to another.

Just write "#include <stdio.h>" and be done with it.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-18-2012
On 7/18/2012 4:39 PM, Raj Pashwar wrote:
> Rather than
> include <stdio.h> can I use int printf(const char *,...) correct at all
> places if I only use printf in my program..


You could, but it would be both silly and dangerous. "Silly"
because the actual declaration in some implementation's <stdio.h>
might be something like

int printf(const char restrict *, ...)
__attribute__((format(printf,1,2)));

.... where the extra stuff enables implementation-specific extra
magic, and by using your own free-hand declaration you might lose
whatever benefits the magic offers. "Dangerous" because strange
things may happen if you get the declaration wrong -- and note that
typing ten more characters than `#include <stdio.h>' gives you ten
additional chances to blunder.

The Fourth Commandment reads

If thy header files fail to declare the return types of
thy library functions, thou shalt declare them thyself
with the most meticulous care, lest grievous harm befall
thy program.

.... and the scholarly annotations thereto say,

The prophet Ansi, in her wisdom, hath added that thou
shouldst also scourge thy Suppliers, and demand on pain
of excommunication that they produce header files that
declare their library functions. For truly, only they
know the precise form of the incantation appropriate to
invoking their magic in the optimal way.

The prophet hath also commented that it is unwise, and
leads one into the pits of damnation and subtle bugs, to
attempt to declare such functions thyself when thy header
files do the job right.

But hey, go ahead, do as you please. As the Sowerberrys so
succinctly put it: "That's - Your - Funeral!"

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Les Cargill
Guest
Posts: n/a
 
      07-18-2012
Raj Pashwar wrote:
> Rather than
> include <stdio.h> can I use int printf(const char *,...) correct at all
> places if I only use printf in my program..
>
> Cheers
>
> ---
> "You are not going to get it right first time" -> Bjarne Stroustrup
>



It's better to use the #include. By "better", I mean that
those who read your code later will see something they're
more likely to expect.

--
Les Cargill

 
Reply With Quote
 
Raj Pashwar
Guest
Posts: n/a
 
      07-19-2012
On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:

> Raj Pashwar <(E-Mail Removed)> writes:
>> Rather than
>> include <stdio.h> can I use int printf(const char *,...) correct at all
>> places if I only use printf in my program..

>
> You *can*, but there's no good reason to do so.
>
> It's permitted by 7.1.4p2 (7.1.7 in C89/C90):
>
> Provided that a library function can be declared without reference
> to any type defined in a header, it is also permissible to declare
> the function and use it without including its associated header.
>
> The <stdio.h> header exists precisely so you don't have write your own
> declarations for standard functions. Using it guarantees (assuming your
> implementation isn't broken) that the declarations will be correct. If
> you write your own declarations, there's always a risk that you'll get
> something wrong.
>
> And in fact, you have. The declaration you show:
>
> int printf(const char *,...);
>
> is correct for C89/C90, but starting with C99 the declaration changed
> to:
>
> int printf(const char * restrict, ...);
>
> (Omitting the "restrict" *probably* won't cause any problems, but it
> could inhibit some optimizations.)
>
> And even that works only because printf doesn't happen to depend on any
> other declarations in <stdio.h>. You can't declare fprintf this way, for
> example, because it depends on the definition of FILE -- and its
> definition can vary substantially from one implementation to another.
>
> Just write "#include <stdio.h>" and be done with it.


Thanks for the informations. Conclusion seems to be that my definition
will work fine though may be sub-optimal.

I see a few reasons to declare the function without the header, ie speeds
up compilation, also allows Principal of Data Hiding (localize
declaration to a block and prevent name leakage elsewhere).

Cheers,
Raj
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      07-19-2012
On 7/19/2012 11:19 AM, Raj Pashwar wrote:
> On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
>>[...]
>> Just write "#include <stdio.h>" and be done with it.

>
> Thanks for the informations. Conclusion seems to be that my definition
> will work fine though may be sub-optimal.
>
> I see a few reasons to declare the function without the header, ie speeds
> up compilation, also allows Principal of Data Hiding (localize
> declaration to a block and prevent name leakage elsewhere).


Have you actually measured a compilation speedup? If so,
how many times must you compile just to recoup the time you've
spent reading and writing on this thread?

As for data hiding: For one thing, printf() is not data, but
a function. For another, an identifier with external linkage will
"leak" to the entire program no matter what you do!

--
Eric Sosman
(E-Mail Removed)d
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-19-2012
Raj Pashwar <(E-Mail Removed)> writes:
> On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
>> Raj Pashwar <(E-Mail Removed)> writes:
>>> Rather than
>>> include <stdio.h> can I use int printf(const char *,...) correct at all
>>> places if I only use printf in my program..

>>
>> You *can*, but there's no good reason to do so.

[30 lines deleted]
>> Just write "#include <stdio.h>" and be done with it.

>
> Thanks for the informations. Conclusion seems to be that my definition
> will work fine though may be sub-optimal.


That may be the conclusion that you drew, but it's certainly not
what I intended. More precisely, your statement is correct, but
not particularly useful.

In particular, if your implementation defines printf with the
"restrict" keyword, and you declare it without "restrict", it may
make your program's behavior undefined (I'd have to dig into the
wording in the standard to be sure). As I said, it's not likely to
affect your program's behavior in practice, but it could -- which
means you have one more thing to worry about when you're trying to
figure out why your program isn't working.

> I see a few reasons to declare the function without the header, ie speeds
> up compilation, also allows Principal of Data Hiding (localize
> declaration to a block and prevent name leakage elsewhere).


You lose the advantages of possible variations in how the
implementation declares printf. The speed-up in compilation speed is
likely to be trivial (have you measured it?), and the time saved is
almost certainly a tiny fraction of the time we've spent discussing
it here. 99% of C programmers just write "#include <stdio.h>";
by doing it differently, you waste the time of anyone reading your
code, because they'll have to figure out what the heck you're doing.

As soon as you add a call to fprintf, you *have* to include the header
because it depends on the definition of FILE. The same applies to any
function that takes a FILE* argument.

Keep it simple. Just write "#include <stdio.h>".

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      07-19-2012
Raj Pashwar <(E-Mail Removed)> writes:
> Rather than
> include <stdio.h> can I use int printf(const char *,...) correct at all
> places if I only use printf in my program..


If you have to ask the question, then, for everyone's safety, just use
the #include.

Phil
--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      07-19-2012
Raj Pashwar <(E-Mail Removed)> writes:
> On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
> > Raj Pashwar <(E-Mail Removed)> writes:

....
> > Just write "#include <stdio.h>" and be done with it.

>
> Thanks for the informations. Conclusion seems to be that my definition
> will work fine though may be sub-optimal.


That's not what I've seen *anyone* recommend, so how can you conclude
that? I do know how how - because it was the answer you wanted, and
you have tacked on "may be sub-optimal" as a way of pretending that
you've paid attention to the answers you've been given by various
highly experienced C programmers. (We must have 100 years between us,
surely?)

> I see a few reasons to declare the function without the header, ie speeds
> up compilation,


I could probably have guessed, given that you asked the question that
you did, that you would also have some unfounded opinion on speed
optimisation. I had it too, for about 2 weeks after learning
C. Fortunately I grew out of it quickly.

> also allows Principal of Data Hiding (localize
> declaration to a block and prevent name leakage elsewhere).


That's not data hiding. That name is not permitted to exist anywhere
except as the standard library defines it, and everyone is permitted
to know what the standard library defines.

You're optimising the wrong thing. Write for future reading, not
for imaginary keyhole optimisations.

Phil
--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
Phil Carmody
Guest
Posts: n/a
 
      07-19-2012
Ben Bacarisse <(E-Mail Removed)> writes:
> Raj Pashwar <(E-Mail Removed)> writes:
>
> > On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
> >
> >> Raj Pashwar <(E-Mail Removed)> writes:
> >>> Rather than
> >>> include <stdio.h> can I use int printf(const char *,...) correct at all
> >>> places if I only use printf in my program..
> >>
> >> You *can*, but there's no good reason to do so.

> <snip details>
> >> Just write "#include <stdio.h>" and be done with it.

> >
> > Thanks for the informations. Conclusion seems to be that my definition
> > will work fine though may be sub-optimal.
> >
> > I see a few reasons to declare the function without the header, ie speeds
> > up compilation, also allows Principal of Data Hiding (localize
> > declaration to a block and prevent name leakage elsewhere).

>
> The second point is rather odd because it conflicts with another: the
> principle of least surprise. C programmer expect printf to be, well,
> the global printf they know, so localising its declaration will surprise
> your readers.


Dang. That's the kind of thing that was going through my head, but
my expression of the idea was pants. You've nailed it though.

Raj - listen to Ben, he speaks wise words.

Phil
--
> I'd argue that there is much evidence for the existence of a God.

Pics or it didn't happen.
-- Tom (/. uid 822)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments