I have tried the following program and I am getting output as a:1 b:3. Can someone help me to understand why this behaviour.
Code:int main() { int a = 1;2;3; int b = (1,2,3); printf("a:%d b:%d",a,b); return 0; }
I have tried the following program and I am getting output as a:1 b:3. Can someone help me to understand why this behaviour.
Code:int main() { int a = 1;2;3; int b = (1,2,3); printf("a:%d b:%d",a,b); return 0; }
Generally, the solution is to avoid writing such code in the first place. The reason is that this:
is the same as this except for formatting:Code:int a = 1;2;3;
That is, you have code that initialises a with 1, and then two lines that have no net effect.Code:int a = 1; 2; 3;
The other thing is that for the expression (1,2,3), the comma operator is in use, which evaluates the left operand, then the right operand, with the result being the value of the right operand. Hence, you end up initialising b with 3. Sometimes this has some use when the left operand has a side effect, but in this case it doesn't so there's no point.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Compile with lots of warnings enabled.
Code:$ gcc -Wall -Wextra foo.c foo.c: In function ‘main’: foo.c:4:14: warning: statement with no effect [-Wunused-value] 4 | int a = 1;2;3; | ^ foo.c:4:16: warning: statement with no effect [-Wunused-value] 4 | int a = 1;2;3; | ^ foo.c:5:14: warning: left-hand operand of comma expression has no effect [-Wunused-value] 5 | int b = (1,2,3); | ^ foo.c:5:16: warning: left-hand operand of comma expression has no effect [-Wunused-value] 5 | int b = (1,2,3); | ^
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
>> Sometimes this has some use when the left operand has a side effect, but in this case it doesn't so there's no point.
Can you please provide an example for the "," usage when the left operand has a side effect?
>> Sometimes this has some use when the left operand has a side effect, but in this case it doesn't so there's no point.
Can you please provide an example for the "," usage when the left operand has a side effect?
The following assignment is giving compilation error(expected identifier)
int a = 1,2,3;
But the following is assigning a to 1.
int a;
a = 1,2,3;
Can you help to understand.
> Can you help to understand.
No. I'm not sure I can, but have a read of Comma in C and C++ - GeeksforGeeks.
After 40 years of C programming this atypical use of the comma has never been a problem, and the compiler generates appropriate warnings and errors when it is.
If it really bothers you, put the 1,2,3 in brackets.
Last edited by hamster_nz; 12-04-2022 at 07:40 PM.
> The following assignment is giving compilation error(expected identifier)
> int a = 1,2,3;
Probably because , is lower precedence than =
C Operator Precedence - cppreference.com
Plus this is valid (see those identifiers).
int a = 1, b = 2, c = 3;
The only time I recall using the comma operator for anything near what it's for is say palindrome checking
Code:for ( int i = 0, j = n-1 ; i < j ; i++, j-- ) { if ( arr[i] == arr[j] ) }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.