S
Shao Miller
main()
{
int d=010;
printf("%d",d);
}
please give with explanation
Correct output: 31 Usenet response posts.
Explanation:
main()
{
int d=010;
printf("%d",d);
}
please give with explanation
Eric Sosman said:Eric Sosman said:On 7/3/2011 12:32 PM, vinay kumar wrote:
main()
{
int d=010;
printf("%d",d);
}
please give with explanation
[snip]
Also either (1) "semi-undefined" because a C90 int-valued
function returns without returning a value, [snip]
Actually this aspect's behavior is defined; only the termination
status returned to the host environment is undefined. (Or it is
implementation-defined in a freestanding implementation.)
To the first point, that's what I intended to indicate with the
weasel-words "semi-defined." The behavior is undefined if (1) the
caller of main() is written in C, and (2) the caller makes use of the
returned value. If the returned value is not used, there's no problem.
And if the caller is not written in C, then C's rules don't apply.
To the second point, in a free-standing implementation it is not
possible to predict (from the information given) what output, if any,
is produced. For all we know, printf() computes checksums of its
arguments, combines them into a complex number, and returns the
logarithm thereof after launching the nuclear missiles.
Keith Thompson said:Tim Rentsch said:You can't take 3 from 2, 2 is less than 3,
so you look at the 4 in the eight's place.
Now that's really four 8's, so you make
it three 8's,
... regroup, and you ...
[...]change an 8 to 8 ones and
add them to the 2 and that gives 1 2 base 8
which is ten base ten and you take away 3 that's 7.
Martin Ambuhl said:Several diagnostics, since
a) main incorrectly has no return type (should be int)
b) printf has no prototype (#include <stdio.h>)
c) for pre-C99 compilers, main fails to return a value.
Tim Rentsch said:Allowed in C90.
C90 - no diagnostic required (and will probably work
just fine).
C90 - no diagnostic required, and behavior is defined
(only termination status returned to host environment
is undefined).
[...]
Reading this reminds me of a quote: "I concede your point -
if things were different, they wouldn't be the same."
Several diagnostics, since
a) main incorrectly has no return type (should be int)
b) printf has no prototype (#include <stdio.h>)
c) for pre-C99 compilers, main fails to return a value.
I know I shouldn't do this, but I'm going to take the bait on this
one. How do you figure the output is the "8".
[...]
Reading this reminds me of a quote: "I concede your point -
if things were different, they wouldn't be the same."
Ass.
But since main is not declared with a return type, this lack of a return
shouldn't trigger a diagnostic, should it?
[snip]
I got the number "8" after I compiled and ran this code on my Linux box
here at home. I just wasn't clear on why the computer produced the
number "8" and not the number "10".
Chad
Correct output: 31 Usenet response posts.
Explanation:
Eric Sosman said:C99: Yes, because defining a function without specifying a
return type requires a diagnostic. (The "lack of a return" need
not produce a diagnostic, and is in fact well-defined for hosted
environments because of a vile pandering to sloppy programmers.
But the "not declared with a return type" is verboten.)
C90: Implementation's discretion. No diagnostic is required,
but all diagnostics are (as always) permitted. ("Warning: Code was
written on a Thursday. I never could get the hang of Thursdays.")
Eric Sosman said:[...]
Reading this reminds me of a quote: "I concede your point -
if things were different, they wouldn't be the same."
Ass.
Keith Thompson said:Tim Rentsch said:Allowed in C90.
C90 - no diagnostic required (and will probably work
just fine).
Right.
But strictly speaking, the behavior is still undefined.
[snip elaboration]
C90 - no diagnostic required, and behavior is defined
(only termination status returned to host environment
is undefined).
Right. But note that, unlike the phrase "undefined behavior", the
standard doesn't say what an undefined termination status means, so we
have to fall back on the English meanings of the words. The most likely
meaning is that the termination status can be as if main() returned any
arbitrary int value (and the implementation is not required to document
how this value is determined, or even to be consistent about it).
But one could imagine the "termination status" being something other
than an int value -- such as the canonical "suffusion of yellow".
[snip C99-related]
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.