For Programmers: Free Programming Magazines  


Home > Archive > C > August 2004 > What's the output?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author What's the output?
Christopher Benson-Manica

2004-08-16, 8:55 am

Can you correctly identify the output of the following program,
without compiling it?

#include <stdio.h>

int main( void )
{
int a=1;
int b=0;
int foo=10+a?1:0+b?1:0;
printf( "%d\n", foo );
return 0;
}

Let's just say that I wish I had opened my desktop C references BEFORE
compiling and checking in certain changes I made. D'oh!

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Artie Gold

2004-08-16, 3:55 pm

Jack Klein wrote:
> On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
> <ataru@nospam.cyberspace.org> wrote in comp.lang.c:
>
>
>
>
> Don't have to. The programmer who wrote that on my team would be
> warned the first time. Transferred or fired if it happened a second
> time.
>

<facetious>
Wouldn't you expect your programmers to understand operator precedence?
</facetious>

<reality>
You are too kind.
</reality>

--ag

--
Artie Gold -- Austin, Texas

20050120->44
Tom St Denis

2004-08-18, 3:55 am

Christopher Benson-Manica wrote:

> Can you correctly identify the output of the following program,
> without compiling it?
>
> #include <stdio.h>
>
> int main( void )
> {
> int a=1;
> int b=0;
> int foo=10+a?1:0+b?1:0;
> printf( "%d\n", foo );
> return 0;
> }
>
> Let's just say that I wish I had opened my desktop C references BEFORE
> compiling and checking in certain changes I made. D'oh!


My initial guess was 1 [and I checked] and I questioned myself thinking 11.

This is why god invented () btw. Write it as

int foo = (10+a)?1:(0+b?1:0);

It becomes much clearer.

Tom
Rich Grise

2004-08-19, 3:55 am

Christopher Benson-Manica wrote:

> Can you correctly identify the output of the following program,
> without compiling it?


Yes. :-)

> #include <stdio.h>
>
> int main( void )
> {
> int a=1;
> int b=0;
> int foo=10+a?1:0+b?1:0;
> printf( "%d\n", foo );
> return 0;
> }
>
> Let's just say that I wish I had opened my desktop C references BEFORE
> compiling and checking in certain changes I made. D'oh!


Wrong, huh? ;-)

Cheers!
Rich

Jack Klein

2004-08-19, 9:11 pm

On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
<ataru@nospam.cyberspace.org> wrote in comp.lang.c:

> Can you correctly identify the output of the following program,
> without compiling it?
>
> #include <stdio.h>
>
> int main( void )
> {
> int a=1;
> int b=0;
> int foo=10+a?1:0+b?1:0;
> printf( "%d\n", foo );
> return 0;
> }
>
> Let's just say that I wish I had opened my desktop C references BEFORE
> compiling and checking in certain changes I made. D'oh!


Don't have to. The programmer who wrote that on my team would be
warned the first time. Transferred or fired if it happened a second
time.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~.../FAQ-acllc.html
Artie Gold

2004-08-19, 9:11 pm

Jack Klein wrote:
> On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
> <ataru@nospam.cyberspace.org> wrote in comp.lang.c:
>
>
>
>
> Don't have to. The programmer who wrote that on my team would be
> warned the first time. Transferred or fired if it happened a second
> time.
>

<facetious>
Wouldn't you expect your programmers to understand operator precedence?
</facetious>

<reality>
You are too kind.
</reality>

--ag

--
Artie Gold -- Austin, Texas

20050120->44
Dave Thompson

2004-08-31, 8:55 pm

On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
<ataru@nospam.cyberspace.org> wrote:

> Can you correctly identify the [result of]
> int foo=10+a?1:0+b?1:0;


Which is why to achieve what you wanted you should have done
int foo = 10+!!a+!!b;
which is read in "English"(?) as ten plus "absolutely the truth value
of a" plus similarly for b <G> <G> <G up SHRT_MAX>.

- David.Thompson1 at worldnet.att.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com