what is the output

E

Eric Sosman

main()
{
int d=010;
printf("%d",d);
}

please give with explanation

Undefined, because a variable-argument function is used
without a prototyped declaration.

Also implementation-defined, because the final line of text
output is not terminated with a newline.

Also either (1) "semi-undefined" because a C90 int-valued
function returns without returning a value, or (2) implementation-
defined because a C99 compiler must diagnose a function definition
without return type, and *if* it translates the program anyhow it
gives it an implementation-defined meaning.

... but the answer you're probably looking for is "8", and
the explanation will be found in the low-numbered pages of your
C textbook -- you know, that unopened volume you've been carrying
around. Open it (it won't bite you), and see what it has to say
about "integer constants" or some similar term.
 
K

Keith Thompson

Eric Sosman said:
Undefined, because a variable-argument function is used
without a prototyped declaration.

Also implementation-defined, because the final line of text
output is not terminated with a newline.

Also either (1) "semi-undefined" because a C90 int-valued
function returns without returning a value, or (2) implementation-
defined because a C99 compiler must diagnose a function definition
without return type, and *if* it translates the program anyhow it
gives it an implementation-defined meaning.

Actually, I think that the behavior of a program that violates a
constraint (given that the compiler chooses to translate it anyway)
is undefined, not implementation-defined.

[...]
 
S

Stefan Ram

vinay kumar said:
main()
{
int d=010;
printf("%d",d);
}

Implicit function declarations have been removed from C,
so the function »printf« cannot be called there.
 
C

Chad

     Undefined, because a variable-argument function is used
without a prototyped declaration.

     Also implementation-defined, because the final line of text
output is not terminated with a newline.

     Also either (1) "semi-undefined" because a C90 int-valued
function returns without returning a value, or (2) implementation-
defined because a C99 compiler must diagnose a function definition
without return type, and *if* it translates the program anyhow it
gives it an implementation-defined meaning.

     ... but the answer you're probably looking for is "8", and
the explanation will be found in the low-numbered pages of your
C textbook -- you know, that unopened volume you've been carrying
around.  Open it (it won't bite you), and see what it has to say
about "integer constants" or some similar term.

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".

Chad
 
O

osmium

:

Undefined, because a variable-argument function is used
without a prototyped declaration.

Also implementation-defined, because the final line of text
output is not terminated with a newline.

Also either (1) "semi-undefined" because a C90 int-valued
function returns without returning a value, or (2) implementation-
defined because a C99 compiler must diagnose a function definition
without return type, and *if* it translates the program anyhow it
gives it an implementation-defined meaning.

... but the answer you're probably looking for is "8", and
the explanation will be found in the low-numbered pages of your
C textbook -- you know, that unopened volume you've been carrying
around. Open it (it won't bite you), and see what it has to say
about "integer constants" or some similar term.

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".

1x8^1 + 0x8^0 = 8
 
K

Keith Thompson

Chad said:
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".

Did you try running the program? What output did you get?

Eric already explained how to find the explanation: see what your C
textbook (or on-line tutorial, or copy of the standard) says about
integer constants.

If you're not interested in doing that, I can explain exactly
why the output is what it is, but you'd learn more by doing your
own research.
 
C

Chad

Did you try running the program?  What output did you get?

Eric already explained how to find the explanation: see what your C
textbook (or on-line tutorial, or copy of the standard) says about
integer constants.

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
 
M

Morris Keesan

....
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".

010 != 10

As someone else suggested, go read what any C reference has to say
about integer constants. Or just google for "C integer constant", and
see what the first result tells you.
 
T

Tim Rentsch

vinay kumar said:
main()
{
int d=010;
printf("%d",d);
}

Re: what is the output
8

please give with explanation

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, 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.

Now instead of 4 in the 8's place, you've got 3
'cause you added 1 (that is to say, 8) to the 2,
but you can't take 7 from 3, so you look at the 64's.

From the 3 you then use 1 to make 8 1's. You add
those 1's to the 3 and you get 1 3 (base 8)
or in other words in base ten you have 11,
and you take away 7,
and 7 from 11 is 4.

Now go back to the 64's, you're left with 2
and you take away 1 from 2 and that leaves??
1, that's right.

....

Remember: base 8 is just like base 10 really.. if
you're missing two fingers.
 
T

Tim Rentsch

Eric Sosman said:
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.)
 
E

Eric Sosman

Eric Sosman said:
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.
 
E

Eric Sosman

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, 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.

Now instead of 4 in the 8's place, you've got 3
'cause you added 1 (that is to say, 8) to the 2,
but you can't take 7 from 3, so you look at the 64's.

"Sixty-four? How did sixty-four get into this?" I hear you
cry. Well, sixty-four is eight squared, don't you see? "Ask a
silly question ..."
 
J

Joe Pfeiffer

Eric Sosman said:
"Sixty-four? How did sixty-four get into this?" I hear you
cry. Well, sixty-four is eight squared, don't you see? "Ask a
silly question ..."

I used to tell my assembly language students I'd come across a good
video about doing arithmetic in various radixes, and then showing them
"New Math" on youtube. The results were wonderful: they had a look of
complete horror when they thought they'd stumbled in to one of those
terrible classes where the professor abdicates the job of delivering
lectures in favor of showing stupid educational videos, with a slow
dawning realization of what they were seeing..... very few of them had
ever heard of Tom Lehrer before that lecture (sadly), but many of them
came to me over the next few days and weeks telling me about "Poisoning
Pigeons in the Park" and "Vatican Rag".

Mission Accomplished.
 
M

Michael Press

Eric Sosman said:
Undefined, because a variable-argument function is used
without a prototyped declaration.

Also implementation-defined, because the final line of text
output is not terminated with a newline.

Also either (1) "semi-undefined" because a C90 int-valued
function returns without returning a value, or (2) implementation-
defined because a C99 compiler must diagnose a function definition
without return type, and *if* it translates the program anyhow it
gives it an implementation-defined meaning.

... but the answer you're probably looking for is "8", and
the explanation will be found in the low-numbered pages of your
C textbook -- you know, that unopened volume you've been carrying
around. Open it (it won't bite you), and see what it has to say
about "integer constants" or some similar term.

I've had some nasty paper cuts from text books.
 
K

Keith Thompson

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.
[...]
 
M

Martin Ambuhl

main()
{
int d=010;
printf("%d",d);
}

please give with explanation

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.
 
K

Keith Thompson

Chad said:
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".

That's probably because you ignored the advice several people have
given, to consult your C reference and see what it has to say about
integer constants.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,952
Messages
2,570,111
Members
46,691
Latest member
Wilhemina5

Latest Threads

Top