Hiii all
I made this program today
int main()
{
int a = 1,2; /* Shows error */
int b = (1,2); /* No error */
}
Why first one shows error while second one does not? Just ( ) makes one program compile. Why?
--Shruti
int a = 1,2;
2
is treated as a variable name which cannot start with a number, hence the error.
int b = (1,2);
comma operator evaluates the operands from left to right and returns the last expression in the list i.e. 2
Inside the parentheses, the language specifies an expression will occur. In that case (b
), the comma represents the comma operator from C.
Without parentheses, the language specifies that variable declarations are separated by commas. In the example of a
, the compiler (parser) is expecting additional variable declarations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With