Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suggest a book for tricky questions in C example unusual if condition [duplicate]

Possible Duplicate:
What does the ',' operator do in C?

Ok I had an interview today and they asked me what should be the output of the following code

#include<stdio.h>

int main ()
{

int a=1,b=1;
char c='0';
if(a,b,c)
   printf("wow \n");
}

after running it on my machine I am able to get the answer but I was not able to answer there.I want to know if such a if statement is allowed? Where is it mentioned?

My problem is the if condition mentioned above I am not able to understand how does that if statement work.

**UPDATE **
I did not found any such thing in K&R can any one recommend a good book.I have programmed things and not new to C but still after failing this question I want to once more look if some more C concepts in depth (specially such as above) are mentioned where can I read.

like image 362
Registered User Avatar asked Jun 18 '11 15:06

Registered User


1 Answers

The comma expression a,b,c just takes the value of the last value, c which has the character value '0', which has a numeric value of 48. So the expression evaluates to true.

like image 82
Node Avatar answered Oct 17 '22 17:10

Node