473,545 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #1
5 1208
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.c yberspace.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.l earn.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.c yberspace.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.c yberspace.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.ne t
Nov 14 '05 #6

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

Similar topics

9
2676
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 doesn't need explanation).
9
2329
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, (using time?). Sims
58
4626
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 (PHONE_LIST, "_+")) { Console.WriteLine (phoneNumber); } } Output: 495.1000
7
1805
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 that when you specify a second argument the number of elements specified in the first argument is each in turn set to the second argument. void...
2
6450
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_ { char szInput; char szOutput; int iSum;
24
2669
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
2875
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 & , int * , int , bool );
23
4833
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
2682
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" xmlns:soapenc="http:// schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/ XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/...
69
3139
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 little pointers, obviously I avoid using them. A few days ago when I was reading a book about programming, I was told that pointers are the very...
0
7487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.