473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

printf("%d%d%d")

Dear all,

Consider:

include <stdio.h>
int main(void)
{
printf("%d%d%d");
return (0);
}

I'm having a bad time trying to explain someone (elsewhere) why the
aforementioned code is a constraint violation and thus requires a
(implementation defined) warning.

I have cited the part of the standard about stdio.h, where they
explicitly state that "If there are insufficient arguments for the
format, the behavior is undefined."

He's arguments have been:
- a undefined behavior doesn't imply a constraint violation
-I can't word out why he's wrong
- gcc doesn't give any warnings using -std=c99
-I'm lost... I have told him that GCC includes a warning switch
(-Wformat) which actually gives a warning for the code above, and that's
fine since the standard just says that the implementation has to give a
warning, but doesn't dictate how, using which flags, ....

Please, do you have any other wording / examples / way to explain this
person why the code is a constraint violation, why it does require a
warning and why GCC is ok by issuing this warning with -Wformat?
Thanks a lot!
--
Pietro Cerutti
Jul 30 '08 #1
12 2117
Pietro Cerutti wrote:
) Consider:
)
) include <stdio.h>
) int main(void)
) {
) printf("%d%d%d");
) return (0);
) }

Undefined behaviour, yes. Reading on...

) I'm having a bad time trying to explain someone (elsewhere) why the
) aforementioned code is a constraint violation and thus requires a
) (implementation defined) warning.
)
) I have cited the part of the standard about stdio.h, where they
) explicitly state that "If there are insufficient arguments for the
) format, the behavior is undefined."
)
) He's arguments have been:
) - a undefined behavior doesn't imply a constraint violation
) -I can't word out why he's wrong

That may be because he is right.
Otherwise, i = i++ would have to generate a diagnostic as well.

) - gcc doesn't give any warnings using -std=c99
) -I'm lost... I have told him that GCC includes a warning switch
) (-Wformat) which actually gives a warning for the code above, and that's
) fine since the standard just says that the implementation has to give a
) warning, but doesn't dictate how, using which flags, ....

If the standard sais that the implementation has to give a diagnostic,
that means you *don't* have to have any extra flags to give said
diagnostic. Just the flags to make it standards conforming.

So he's right again.

) Please, do you have any other wording / examples / way to explain this
) person why the code is a constraint violation, why it does require a
) warning and why GCC is ok by issuing this warning with -Wformat?

"You were right, it's not a constraint violation." would be adequate. ;-)
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jul 30 '08 #2
Willem wrote:
Pietro Cerutti wrote:
) Consider:
)
) include <stdio.h>
) int main(void)
) {
) printf("%d%d%d");
) return (0);
) }

Undefined behaviour, yes. Reading on...

) I'm having a bad time trying to explain someone (elsewhere) why the
) aforementioned code is a constraint violation and thus requires a
) (implementation defined) warning.
)
) I have cited the part of the standard about stdio.h, where they
) explicitly state that "If there are insufficient arguments for the
) format, the behavior is undefined."
)
) He's arguments have been:
) - a undefined behavior doesn't imply a constraint violation
) -I can't word out why he's wrong

That may be because he is right.
Otherwise, i = i++ would have to generate a diagnostic as well.
It does, using at least -Wsequence-point.
>
) - gcc doesn't give any warnings using -std=c99
) -I'm lost... I have told him that GCC includes a warning switch
) (-Wformat) which actually gives a warning for the code above, and that's
) fine since the standard just says that the implementation has to give a
) warning, but doesn't dictate how, using which flags, ....

If the standard sais that the implementation has to give a diagnostic,
that means you *don't* have to have any extra flags to give said
diagnostic. Just the flags to make it standards conforming.
Sais who? The standard says "in an implementation defined manner". It
doesn't dictate any further. Am I missing anything?

SaSW, Willem

--
Pietro Cerutti
Jul 30 '08 #3
Pietro Cerutti wrote:
) Willem wrote:
)That may be because he is right.
)Otherwise, i = i++ would have to generate a diagnostic as well.
)
) It does, using at least -Wsequence-point.

Is there a warning flag for *all* undefined behaviour ?
Note the following piece of undefined behaviour code:

#include <stdio.h>
int main(void)
{
char x[20] = "one one one one";
char y[20] = "two two two two";
int i = 0;
for (i = 0; i < 40; i++)
x[i] = 'x';
printf("%s, %s",x , y);
return 0;
}

(Interestingly, on my implementation, this has different results when
compiled with or without optimization (gcc 4.2.1 for the record))

)>
)) - gcc doesn't give any warnings using -std=c99
)) -I'm lost... I have told him that GCC includes a warning switch
)) (-Wformat) which actually gives a warning for the code above, and that's
)) fine since the standard just says that the implementation has to give a
)) warning, but doesn't dictate how, using which flags, ....
)>
)If the standard sais that the implementation has to give a diagnostic,
)that means you *don't* have to have any extra flags to give said
)diagnostic. Just the flags to make it standards conforming.
)
) Sais who? The standard says "in an implementation defined manner". It
) doesn't dictate any further. Am I missing anything?

You're making a big leap when you interpret "in an implementation defined
manner" as "you may need to specify additional options".

It simply means that the form of the diagnostic is up to the
implementation. But it *must* issue *something*.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jul 30 '08 #4
Pietro Cerutti <gahr_SPAM_gahr_ME_chsaid:
Willem wrote:
<snip>
>Otherwise, i = i++ would have to generate a diagnostic as well.

It does, using at least -Wsequence-point.
It's allowed to. It just doesn't *have* to. The behaviour (if indeed there
is any) of code that contains a constraint violation or syntax error is
undefined - all cats and dogs are animals - but undefined behaviour is not
sufficient on its own to qualify as a constraint violation or syntax error
requiring a diagnostic message - not all animals are cats or dogs.

<snip>
>If the standard sais that the implementation has to give a diagnostic,
that means you *don't* have to have any extra flags to give said
diagnostic. Just the flags to make it standards conforming.

Sais who? The standard says "in an implementation defined manner". It
doesn't dictate any further. Am I missing anything?
Yes. Firstly, if the implementation is not invoked in conforming mode
(whether it's through flags or semaphore messages across the mountains or
telepathy or is simply the default invocation mode for the
implementation), we're not discussing C, we're off-topic, and the Standard
is meaningless within the context of the discussion. So we're stuck with
conforming mode. Given conforming mode, if a particular translation unit
contains any syntax errors or constraint violations, at least one
diagnostic message is *required*. In that and any other circumstance, any
other diagnostic messages are *allowed*.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '08 #5
Richard Heathfield wrote:
Pietro Cerutti <gahr_SPAM_gahr_ME_chsaid:
>Willem wrote:
<snip>
>>Otherwise, i = i++ would have to generate a diagnostic as well.
It does, using at least -Wsequence-point.

It's allowed to. It just doesn't *have* to. The behaviour (if indeed there
is any) of code that contains a constraint violation or syntax error is
undefined - all cats and dogs are animals - but undefined behaviour is not
sufficient on its own to qualify as a constraint violation or syntax error
requiring a diagnostic message - not all animals are cats or dogs.
Oh I see.. so I was wrong in thinking that UB implies constraint
violation. One point is not clear to me, which I would kindly ask you to
clarify...

Given this:
3.8
1 constraint
restriction, either syntactic or semantic, by which the exposition
of language elements is to be interpreted

How can a call to printf("%d%d%d") not fail to conform to a semantic
exposition of the language, in this case of the standard function?

That is why I thought that such a call was a constraint violation, in
the first place.

--
Pietro Cerutti
Jul 30 '08 #6
Pietro Cerutti <gahr_SPAM_gahr_ME_chwrote:
Richard Heathfield wrote:
>Pietro Cerutti <gahr_SPAM_gahr_ME_chsaid:
>>Willem wrote:
<snip>
>>>Otherwise, i = i++ would have to generate a diagnostic as well.
It does, using at least -Wsequence-point.

It's allowed to. It just doesn't *have* to. The behaviour (if indeed
there is any) of code that contains a constraint violation or syntax
error is undefined - all cats and dogs are animals - but undefined
behaviour is not sufficient on its own to qualify as a constraint
violation or syntax error requiring a diagnostic message - not all
animals are cats or dogs.

Oh I see.. so I was wrong in thinking that UB implies constraint
violation. One point is not clear to me, which I would kindly ask you
to clarify...

Given this:
3.8
1 constraint
restriction, either syntactic or semantic, by which the
exposition of language elements is to be interpreted

How can a call to printf("%d%d%d") not fail to conform to a semantic
exposition of the language, in this case of the standard function?
This is because printf is a variadic function and it is not always
possible for the compiler to check whether you have supplied the
corresponding arguments for any format specifiers in your format
string. Therefore calling printf with insufficient arguments is
undefined behaviour but not a constraint violation, so a diagnostic[s]
is allowed, but not necessary.
That is why I thought that such a call was a constraint violation, in
the first place.
Jul 30 '08 #7
Pietro Cerutti <gahr_SPAM_gahr_ME_chwrote:
Dear all,

Consider:

include <stdio.h>
int main(void)
{
printf("%d%d%d");
return (0);
}

I'm having a bad time trying to explain someone (elsewhere) why the
aforementioned code is a constraint violation and thus requires a
(implementation defined) warning.
That would be because it isn't and doesn't.
I have cited the part of the standard about stdio.h, where they
explicitly state that "If there are insufficient arguments for the
format, the behavior is undefined."
Quite. Undefined, not:

Constraint
1. There shall be sufficient arguments for the format.

He's arguments have been:
- a undefined behavior doesn't imply a constraint violation
-I can't word out why he's wrong
He's right.

Richard
Jul 30 '08 #8
Pietro Cerutti <gahr_SPAM_gahr_ME_chsaid:

<snip>
One point is not clear to me, which I would kindly ask you to
clarify...

Given this:
3.8
1 constraint
restriction, either syntactic or semantic, by which the exposition
of language elements is to be interpreted

How can a call to printf("%d%d%d") not fail to conform to a semantic
exposition of the language, in this case of the standard function?
Rather than repeat santosh's perfectly correct answer, I'll give you a
slightly different perspective.

You have quite rightly spotted that C99 defines "constraint", but I wonder
whether you have noticed that each and every constraint is documented at
the head of the relevant section, under a subsection entitled, in each
case, "Constraints". It is violation of these constraints that requires a
syntax error. So - if we believe that printf("%d%d%d") violates a
constraint, it is reasonable to ask /which/ constraint is violated. I
can't find any constraint that printf("%d%d%d") violates. Can you?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '08 #9
Richard Heathfield said:

<snip>
It is violation of these
constraints that requires a syntax error.
s/syntax error/diagnostic message/furrfu!

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 30 '08 #10
Richard Heathfield wrote:
Pietro Cerutti <gahr_SPAM_gahr_ME_chsaid:

<snip>
>One point is not clear to me, which I would kindly ask you to
clarify...

Given this:
3.8
1 constraint
restriction, either syntactic or semantic, by which the exposition
of language elements is to be interpreted

How can a call to printf("%d%d%d") not fail to conform to a semantic
exposition of the language, in this case of the standard function?

Rather than repeat santosh's perfectly correct answer, I'll give you a
slightly different perspective.

You have quite rightly spotted that C99 defines "constraint", but I wonder
whether you have noticed that each and every constraint is documented at
the head of the relevant section, under a subsection entitled, in each
case, "Constraints". It is violation of these constraints that requires a
syntax error. So - if we believe that printf("%d%d%d") violates a
constraint, it is reasonable to ask /which/ constraint is violated. I
can't find any constraint that printf("%d%d%d") violates. Can you?
Given your explanation, no, I can't. Indeed, I didn't notice that
constraints were explicitly stated in their own subsections. Thanks!

--
Pietro Cerutti
Jul 30 '08 #11
On 30 Jul, 10:11, Pietro Cerutti <gahr_SPAM_gahr_ME_chwrote:
Richard Heathfield wrote:
Pietro Cerutti <gahr_SPAM_gahr_ME_chsaid:
Willem wrote:
>Otherwise, i = i++ would have to generate a diagnostic as well.
It does, using at least -Wsequence-point.
It's allowed to. It just doesn't *have* to. The behaviour (if indeed there
is any) of code that contains a constraint violation or syntax error is
undefined - all cats and dogs are animals - but undefined behaviour is not
sufficient on its own to qualify as a constraint violation or syntax error
requiring a diagnostic message - not all animals are cats or dogs.

Oh I see.. so I was wrong in thinking that UB implies constraint
violation. One point is not clear to me, which I would kindly ask you to
clarify...

Given this:
3.8
1 constraint
restriction, either syntactic or semantic, by which the exposition
of language elements is to be interpreted

How can a call to printf("%d%d%d") not fail to conform to a semantic
exposition of the language, in this case of the standard function?

That is why I thought that such a call was a constraint violation, in
the first place.
it's impossible to diagnose in general. (at compile time that is).

/* print.c */
#include <stdio.h>

int main(void)
{
char s [80];
fgets (s, sizeof s, stdin);
printf (s);
return 0;
}

print %d%d%d

in general the C standard says things have Undefined Behaviour
if they cannot be easily detected at compile time. UB does *not*
require a diagnostic and is *not* a constraint violation.

gcc can diagnose *some* printf/scanf errors but it is
not required to. Some compilers don't do any checks
on format strings.
--
Nick Keighley

Jul 30 '08 #12
>include <stdio.h>
>int main(void)
{
printf("%d%d%d");
return (0);
}
>I have cited the part of the standard about stdio.h, where they
explicitly state that "If there are insufficient arguments for the
format, the behavior is undefined."

He's arguments have been:
- a undefined behavior doesn't imply a constraint violation
-I can't word out why he's wrong
>- gcc doesn't give any warnings using -std=c99
-I'm lost... I have told him that GCC includes a warning switch
(-Wformat) which actually gives a warning for the code above, and that's
fine since the standard just says that the implementation has to give a
warning, but doesn't dictate how, using which flags, ....
From the standard's point of view, every combination of compiler
flags that affects warning messages, code generation, or other
requirements imposed by the standard is a different implementation.
There is not one implementation of GCC. Any given copy likely has
more than 2**200 different implementations, selected with compiler
flags. And that doesn't include different versions or different
target machines. However, you only have to be concerned with the
ones claimed to be standards-conforming.
>Please, do you have any other wording / examples / way to explain this
person why the code is a constraint violation,
It isn't.
>why it does require a
warning
It doesn't.
>and why GCC is ok by issuing this warning with -Wformat?
Two implementations that differ only in the -Wformat flag are equally
conforming. The warning isn't required. However, gcc is permitted
to issue a warning that is not required anyway (and regardless of
how factually wrong, stupid, irrelevant, or pointless it is).
Examples:

warning: it's Tuesday.
warning: division by 3 (even if program contains no division)
warning: program does not contain a license.

Jul 30 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: sugaray | last post by:
does the expression int a=printf("%d\n",a); implementation dependent ? I supposed it would produced the result 2, but i got 4206596 (result may vary on your machine), i wonder if the value produced...
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
12
by: baumann | last post by:
hi all, printf("%c",b) doesn't work properly. #include <stdio.h> int a , b; char d, e; char * p; float f; int main(int argc, char* argv) {
9
by: geek | last post by:
Hi all, Why does a printf("%d") statement prints this as output and when I do printf("%c") it doesn't print anything. -13361016 Any help would be appreciated. Thanks,
19
by: v4vijayakumar | last post by:
why the following statement dumps the core(Segmentation fault)? printf("%s\n", __FILE__);
12
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: ...
10
by: lovecreatesbeauty | last post by:
Is parameter type conversion required for the 2nd argument on printf("%p", (void *)&i); ? But one would never call memcpy like: memcpy((void *)pi, (void *)pj, sizeof *pj); /*memcpy((void *)pi,...
26
by: Yevgen Muntyan | last post by:
Hey, It was mentioned elsewhere that printf("%d", INT_MAX); is implementation-defined. Why? Is it because INT_MAX value is implementation-defined so output depends on implementation, or is it...
7
by: Rajesh S R | last post by:
printf("%hhd",89);/*Assume char has 8 bits and is signed*/ Is it valid? I know that it has been discussed in comp.std.c. ...
29
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.