code 1:

#include<stdio.h>
int main(void)
{
int a;
a= 1,2,3;
int b = a++;
printf("%d",b);
return 0;
}

code 2 :

#include<stdio.h>
int main(void)
{
int a;
a= (1,2,3);
int b = a++;
printf("%d",b);
return 0;
}

Can you please explain why that bracket is making ouput changed ? i know comma operator wrokd from left to right. but why that thing is not working for first case ? thanks alot in advance.

what i am thinking is that = has more precedence than comma. so it is attached to that more tightly. but then is it looking like this.

((((a=1),2),3));

so after assigning them, why 2,3 is acceptable to compiler ? why is it nor giving error ? thanks.

It looks like you are trying to initialize an array of integers. From the way you have coded it, the compiler should have at least issued a loud warning. In any case, you don't show what your output is... :-(

sir, i have changed the question right now, please look at the change as i was editing when you have answered. thanks. sorry for that! o/p is 1,3 respectively and no warning at all. here is the proof. Click Here thanks

Member Avatar for iamthwee

Compiling with the -Wall and -pedantic switches...

$ gcc -Wall -pedantic blah.cpp
blah.cpp: In function ‘int main()’:
blah.cpp:5:7: warning: left-hand operand of comma has no effect
blah.cpp:5:9: warning: right-hand operand of comma has no effect

so what is the answer now ? but have u seen the link which i have given ? it is showing no error. reson may be that u are using .cpp file and i am using .c file. (i am not sure though). then what is the correct and exact answer for the question if you are not goven anything except that code snippet ? thanks.

Member Avatar for iamthwee

Compiling with .c extension also yeilds:

$ gcc -Wall blah.c
blah.c: In function ‘main’:
blah.c:5:6: warning: left-hand operand of comma expression has no effect
blah.c:5:8: warning: left-hand operand of comma expression has no effect

ohh... these are warning. but not errors i suppose. so @iamthewee what will you say about the codes now ? and why are using switches to compile the codes ? thanks.

comma works as an operator in some cases and as a separator in other cases.
when it works as an operator (like in ur code 2): it evaluates the first operand and discards the result, evluates the next operand and discards the result.. and so on till it reaches the last operand whose result/value/type is returned.
example:

int a=(f(),g());

here, f() is first called and evaluated. but its result is not taken care of. then g() is called and evaluated. it result is assigned to a.

that is what is happening in ur code 2.

now coming to code 1:(here comma works as a separator):
its used in variable declarations,function calls,...etc

like

int a=1,b=2;

means that

int a=1;
int b=2;

going by the same concept in ur code 1:
a=1
your doubt was that what happens to the remaining 2,3 in ur code 1. well! when comma works as a separator then it doesnt care much... it just separates things. hope u get it although i havent been able to explain code 1 properly.

and never confuse comma operator with comma separator.
example:

void f(g(),h());

here it works as a separator.

also if u would have written

int a=1,2,3;

then it would have given error as it would mean

int a=1;
int 2;
int 3;

coz here comma works as a separator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.