Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   is this declaration correct (http://www.velocityreviews.com/forums/t948243-is-this-declaration-correct.html)

Raj Pashwar 07-18-2012 08:39 PM

is this declaration correct
 
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

Keith Thompson 07-18-2012 09:09 PM

Re: is this declaration correct
 
Raj Pashwar <raj121190@hotmail.NOSPAM.com> 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) kst-u@mib.org <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"

Eric Sosman 07-18-2012 09:16 PM

Re: is this declaration correct
 
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
esosman@ieee-dot-org.invalid

Les Cargill 07-18-2012 10:54 PM

Re: is this declaration correct
 
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


Raj Pashwar 07-19-2012 03:19 PM

Re: is this declaration correct
 
On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:

> Raj Pashwar <raj121190@hotmail.NOSPAM.com> 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

Eric Sosman 07-19-2012 03:53 PM

Re: is this declaration correct
 
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
esosman@ieee-dot-org.invalid

Keith Thompson 07-19-2012 05:19 PM

Re: is this declaration correct
 
Raj Pashwar <raj121190@hotmail.NOSPAM.com> writes:
> On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
>> Raj Pashwar <raj121190@hotmail.NOSPAM.com> 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) kst-u@mib.org <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"

Phil Carmody 07-19-2012 06:08 PM

Re: is this declaration correct
 
Raj Pashwar <raj121190@hotmail.NOSPAM.com> 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)

Phil Carmody 07-19-2012 07:36 PM

Re: is this declaration correct
 
Raj Pashwar <raj121190@hotmail.NOSPAM.com> writes:
> On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
> > Raj Pashwar <raj121190@hotmail.NOSPAM.com> 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)

Phil Carmody 07-19-2012 08:46 PM

Re: is this declaration correct
 
Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
> Raj Pashwar <raj121190@hotmail.NOSPAM.com> writes:
>
> > On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson wrote:
> >
> >> Raj Pashwar <raj121190@hotmail.NOSPAM.com> 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)


All times are GMT. The time now is 08:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.