I know that multiple variables belonging to same data type can be declared in one statement as below:
int x, y, z;
I tried to declare and define multiple variables belonging to same data-type in one statement as below :
#include <stdio.h>
int main() {
int x = y = z = 50;
printf("%d", x + y + z);
return 0;
}
When I tried to compile above code I got following errors:
prog.c: In function ‘main’:
prog.c:4:11: error: ‘y’ undeclared (first use in this function)
4 | int x = y = z = 50;
| ^
prog.c:4:11: note: each undeclared identifier is reported only once for each function it appears in
prog.c:4:15: error: ‘z’ undeclared (first use in this function)
4 | int x = y = z = 50;
| ^
I don't understand why multiple variables belonging to same data type cannot be initialized to the same value in a single statement, whereas it is possible to declare multiple variables belonging to same data type.
I also don't understand the actual meaning of the note "each undeclared identifier is reported only once for each function it appears in." Why is it appearing?
Though, when I tried below code where I created two statements for variable declaration and definition separately everything worked smoothly. Why so?
#include <stdio.h>
int main() {
int x, y, z;
x = y = z = 50;
printf("%d", x + y + z);
return 0;
}
And got expected below output:
150
int x = y = z = 50;
The assignment =
operator has right-to-left associativity1. The above statement is parsed as:
int x = (y = (z = 50));
which first tries to assign 50 to the L-value z
, then assigns the result of z
converted to the type of y
to the L-value y
, and then initializes the L-value x
with the result of y
converted to the type of x
. But y
and z
were never declared before, so it generated a warning.
The alternative expression, if it was to be parsed as such:
(((int x = y) = z) = 50);
would raise an error because the expression x = y
is not an L-value, i.e. it has an R-value but not an L-value where to store the result of z
.
I don't understand why multiple variables belonging to same data type cannot be initialized to the same value in a single statement, whereas it is possible to declare multiple variables belonging to same data type.
Actually you can, but only if you declare them first. In this case, you didn't declare y
and z
before initializing them.
What you're looking for is:
1) int x = 50, y = 50, z = 50;
/* OR */
2) int x, y, z;
x = y = z = 50;
/* Or */
3) int x = 50;
int y = 50;
int z = 50;
/* Or */
4) int x, y, z = x = y = 50; /* This is madness. */
I find that declaring one variable per line (the 3rd approach) makes the code clearer and prevents asinine mistakes when declaring pointers.
I also don't understand the actual meaning of the note "each undeclared identifier is reported only once for each function it appears in." Why is it appearing?
It means that the compiler will only report the first instance of an undeclared variable in a given function. For instance, if you have:
int x = y = z = 50; /* Compiler only reports this instance */
z = 403;
The compiler will only report the first time z
appears.
1
Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same precedence, in the given direction. For example, the expression a=b=c is parsed as a=(b=c), and not as (a=b)=c because of right-to-left associativity.