Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Code

Status
Not open for further replies.

whiteninja

IS-IT--Management
Jun 18, 2007
12
TT
Could anyone please explain following code and its output.

printf(“%d”,’8’==’5+3’);



Thanks
 
Do you have a result you can't explain?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
No - It came as a question in one of my C exams.
 
I wonder how badly worded the rest of the exam was.

Variously, the answer is
- syntax error, illegal wide character constant
- nothing at all
- 0


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Not that it is very timely, the answer is that it would print a 0 (zero). The statement:
printf("%d", '8'=='5+3');
Prints a decimal number (%d) and the decimal number it prints is actually a logical compare of the single character 8 ('8') and the single character '5+3' which is not really a character at all. The logical compare '==' results in a false which equates to 0 (zero).
 
In actual fact it's unknown result of '8' == '5+3' because "A multicharacter literal has type int and implementation-defined value" (The C++ Standard). We don't know what's C++ implementation assumed...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top