No compilation and runtime errors
Is 2019 is kind of variable or anything? Explain this please
#include <stdio.h>
int main()
{
int a, b;
a= b = 2020, 2019;
printf("%d %d", a, b);
return 0;
}
No compilation and runtime errors
Is 2019 is kind of variable or anything? Explain this please
#include <stdio.h>
int main()
{
int a, b;
a= b = 2020, 2019;
printf("%d %d", a, b);
return 0;
}
a = b = 2020, 2019;
Is parsed as:
(a = (b = 2020)), 2019;
The ,
is a comma operator. First the left side of ,
is executed, it's result is discarded, a sequence point happens and then the right side is executed. So let's execute the left side:
First the inner braces need to be executed, ie. b = 2020
is executed, ie. b
is assigned the value of 2020
. The simple assignment operator =
"returns" the same value that was stored, ie. in this case b = 2020
returns the value 2020
. So now we have:
(a = 2020), 2019;
Then a = 2020
is executed - a
is assigned the value of 2020
. So now we have:
2020, 2019;
The result of the left side of comma operator is ignored (and side effects happen). So now we have:
2019;
It's just a value, something one could call an "empty statement" or "empty expression". A single value doesn't do anything, it is permitted by the language grammar and is like just ignored. Mostly empty statements with a (void)
cast are used to remove compilers warnings about unused variables, they are used like int unused; (void)unused;
- the (void)unused
is just an expression that doesn't do anything.
Can you explain what happens with 2019
Nothing.
and what it is?
A rvalue of type int
. A expression that has type int
and value 2019
.
Is 2019 is kind of variable or anything?
It's not a variable, it's just an expression. Try it out, any number is an expression, like int main() { 1; 2; 3; 1.0; 1 + 1; }