Why does this code not throw a compilation error for y being undeclared?
int x = 10, y;
printf("%d", y);
There's no expression like int y;
. In my case, the console print out is 32764
, which seems to just be uninitialized memory. Looking at the assembly code for the first line, it's the same whether the , y
is there or not, even if y is used in the print statement.
Expected to see
error: use of undeclared identifier 'y' printf("%d", y);
CodePudding user response:
This:
int x = 10, y;
Is not an instance of the comma operator. The ,
is part of the grammar of a declaration which allows multiple variables to be declared on the same line. Specifically, it declares x
and initializes it to 10 and declares y
which is uninitialized. It's equivalent to:
int x = 10;
int y;
Had you instead done this:
int x = (10, y);
Then you would have an instance of the comma operator and an error for an undeclared identifier.