7

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.

Community
  • 1
  • 1
Registered User
  • 5,173
  • 16
  • 47
  • 73
  • 4
    The result of the comma expression is the value of the last expression. Which is '0' which is not zero. – Hans Passant Jun 18 '11 at 15:46
  • 2
    @Hans: Seriously... writing the same text in a different text box (for example the one reserved for answers) is not a hard thing to do... – rubenvb Jun 18 '11 at 15:57
  • Its the same as `if (c)` - have a look at the compile warnings in [this example](http://ideone.com/xuPaZ). – Node Jun 18 '11 at 16:01
  • I see the program is C99 (no `return` in main); but C99 didn't change the legal signatures for `main`. Valid signatures are `int main(void)` and `int main(int argc, char **argv)` -- the program is invalid: anything can happen :) – pmg Jun 18 '11 at 16:02
  • @ruben - it is, SO doesn't permit posting short answers. – Hans Passant Jun 18 '11 at 16:12
  • @Hans: see my new answer as proof to your laziness. – rubenvb Jun 18 '11 at 16:18

7 Answers7

4

Have a look at the comma operator.

As you can see, the evaluation of e1, e2, e3 is e3, as specified in the ANSI C standard

so, your if condition evaluates in '0' wich is the 0 charcater, wich have a VALUE different from 0, so, the condition is true, and 'wow' is printed

Community
  • 1
  • 1
ArtoAle
  • 2,939
  • 1
  • 25
  • 48
4

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.

Node
  • 3,443
  • 16
  • 18
2

Wikipedia's entry on the comma operator is quite good, it explains how it works concisely.

The result of the expression a, b, c is c which in this case is equal to '0', which evaluates to true.

Jon
  • 428,835
  • 81
  • 738
  • 806
1

Comma expression. The result of it is the rightmost arguement, in your case - c. It is also notable that the comma expression guarantees a sequence point, that is the arguments are evaluated from left to right, unlike many other operators

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • As you consider function parameters, comma is just a separator. So you cannot make sure the evaluation sequence. Therefore once you evaluate the expression like foo(a(), b());, you cannot know which function (a(), b()) has been invoked first. – albin Aug 09 '13 at 20:01
0

The comma operator is a C++ operator that has the effect of evaluating all the expressions, and discarding the result of all the expressions but the last.

In your case, both the following statements are equivalent.

if (a,b,c) {
 printf("wow \n");
}

if (c) {
 printf("wow \n");
}

In both the cases, the code inside the IF-statement will be executed basing on the value of c.

apaderno
  • 28,547
  • 16
  • 75
  • 90
  • kiamlaluno some people are mentioning above in replies that if will be evaluated on basis of c and not a. – Registered User Jun 18 '11 at 15:54
  • @Registered User And that is correct. The comma operator is evaluated left to right, and the value of the last evaluated expression is the value associated to the full expression. – apaderno Jun 18 '11 at 15:59
  • @pmg It is what I read in my answer, now. – apaderno Jun 18 '11 at 16:00
  • They are not strictly equivalent in the general case: imagine `if (a = 1, b = 2, c = 3) {}` and `if (c = 3) {}` (ignore the warning a good compiler should emit for the snippets) – pmg Jun 18 '11 at 16:05
  • 1
    @pmg That is why I wrote "in your case." – apaderno Jun 18 '11 at 16:06
0

The result of the comma expression is the value of the last expression. Which is '0' which is not zero.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
0

It's getting old now and the author makes a lot of reference to the Solaris environment and compiler, but you could look at 'Expert C Programming: Deep C Secrets' by Peter Van Der Linden. At least 90% of the book is still very useful. There is even an appendix on interview questions. I can't remember if the comma operator is covered but many other things are.

Two other (free) resources: http://c-faq.com/

http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf

thelionroars1337
  • 188
  • 1
  • 3
  • 10