Keith Thompson said:
Tim Rentsch said:
Keith Thompson said:
On Wed, 18 Jul 2012 14:09:01 -0700, Keith Thompson 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 *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). [snip]
Now that it is being pointed out, I'm confident you will
find the relevant passage or passages and post a correction.
Not exactly. [snip long and careful response]
I apologize. My response should have been more direct.
The declarations
int printf( const int *, ... );
int printf( const int * restrict, ... );
(You mean const char *, not const int *, yes?)
Yes I did, thank you (although it doesn't change the point about
using 'restrict').
Thanks, I should have found that myself.
But look at 6.7.6p2:
Each declarator declares one identifier, and asserts that
when an operand of the same form as the declarator appears
in an expression, it designates a function or object with the
scope, storage duration, and type indicated by the declaration
specifiers.
The types of
int printf( const char *, ... );
int printf( const char * restrict, ... );
are *compatible*, but they aren't *the same*, which is what 6.7.6p2
requires.
The key question here is what type is "indicated". The answer to
that is specified in 6.2.7 p4:
For an identifier with internal or external linkage declared
in a scope in which a prior declaration of that identifier
is visible, if the prior declaration specifies internal or
external linkage, the type of the identifier at the later
declaration becomes the composite type.
Note that the last sentence of 6.7.6.3 p15 also applies to the
formation of the composite type in this case.
6.7.6p2 says that the declaration "asserts" that they're the same
type. It doesn't directly say what happens if that assertion fails;
I presume the behavior is undefined.
The type being asserted simply changes as the program text
proceeds. I don't think there is anything very mysterious
about this. If we have code like
int a[];
...
a[0] = 0;
...
int a[10];
a[0] = 1;
the type of 'a' is incomplete at the first assignment, and
complete at the second assignment. There is nothing that
requires these two types to be the same; each declarator
asserts what it asserts until (possibly) changed by a
subsequent declarator.
Is there a clearer statement about the requirement for declarations to
match definitions? For example, if I declare
int printf(const char*); /* note: non-variadic */
and then call printf("Hello, world\n"), the behavior is undefined; is
this explicitly stated somewhere?
The call is UB because of 6.5.2.3 p9.
The declaration is UB because of 6.2.7 p2.
Because printf() is called, 6.9 p5 requires that printf() be
defined (or else UB).
Of course, there is the question of how printf() is defined,
hosted implementation, yada yada yada. But you know all
that.
Ok, but I think the stylistic advice is important in this case. My
impression of your response to the OP was that didn't sufficiently
emphasize that part.
I am perfectly okay with different people having different opinions
on points of style. Generally I try to avoid style battles whenever
possible. What bothered me, I think, was so many people focusing on
the style concerns at the expense of what the OP really was trying to
ask about. I feel like his questions, and what intentions motivated
those questions, got lost in the noise of giving style advice. As
long as that point is addressed, clearly and distinctly, I'm okay
with however much emphasis people want to give on the style issues;
in fact how much style concerns are emphasized is itself a kind of
style issue. On this particular issue I think our goals are actually
not that far apart; where we may differ is in what approach will
most help achieve those goals. You might be interested to know that
partly I was inspired by an aphorism from another posting of yours
(on a different topic, not that that matters), which was "when in
doubt, learn the rules, thereby removing the doubt" (or something
along those lines anyway). I think that's an excellent aphorism,
and was pleased that OP had (unconsciously) adopted it.
I hope you've enjoyed my comments even if we haven't yet
reached 100% agreement.