473,382 Members | 1,425 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,382 software developers and data experts.

What's the output?

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.
Nov 14 '05 #1
5 1202
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
Nov 14 '05 #2
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

Nov 14 '05 #3
On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
<at***@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/~a...FAQ-acllc.html
Nov 14 '05 #4
Jack Klein wrote:
On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
<at***@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.

<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
Nov 14 '05 #5
On Fri, 13 Aug 2004 15:55:15 +0000 (UTC), Christopher Benson-Manica
<at***@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
Nov 14 '05 #6

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

Similar topics

9
by: Glutinous | last post by:
Trying to learn php, and came across =& as in $form =& $HTTP_POST_VARS; I can't find any reference to =&, with no success in searching for a definition (as though it's _so_ obvious it...
9
by: Sims | last post by:
Hi, I recall a function that would tell me how long it took to generate an output. But for the life of me I cannot remember it. Dow anybody know it? Or do I have to write something myself,...
58
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
7
by: Neil Zanella | last post by:
OK, this time the compiler's got me a little bit puzzled, simply because it is doing something I am not expecting. My understanding, according to the documentation of std::vector<>::resize(), is...
2
by: yee young han | last post by:
I need a fast data structure and algorithm like below condition. (1) this data structure contain only 10,000 data entry. (2) data structure's one entry is like below typedef struct _DataEntry_...
24
by: kalamantina | last post by:
#include "stdafx.h" #include <stdio.h> #define output( x ) printf( #x "\r\n" );fflush( stdout ) class CMyBase { public: CMyBase() { output( CMyBase() ); f(*this);
1
by: aboood | last post by:
Hi guys Could any one tell me what does that code do please? in pseudo-code #include <fstream.h> #include <iostream.h> bool differsByOneDigit ( int , int ); void outputResults ( ofstream & ,...
23
by: mahesh | last post by:
Hi all, I have following code that is supposed to increase the power by specified value. int main() { system("cls"); int i, exponent; double base; double new_base=0.0;
0
by: Philluminati | last post by:
I have a Perl SOAP Server which returns this SOAP Message when invoked: <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:xsi="http:// www.w3.org/2001/XMLSchema-instance"...
69
by: Yee.Chuang | last post by:
When I began to learn C, My teacher told me that pointer is the most difficult part of C, it makes me afraid of it. After finishing C program class, I found that all the code I wrote in C contains...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.