What is the output of the following code:
int main() {
int k = (k = 2) + (k = 3) + (k = 5);
printf("%d", k);
}
It does not give any error, why? I think it should give error because the assignment operations are on the same line as the definition of k.
What I mean is int i = i; cannot compile.
But it compiles. Why? What will be the output and why?
int i = icompiles because 3.3.1/1 (C++03) saysSo
iis initialized with its own indeterminate value.However the code invokes Undefined Behaviour because
kis being modified more than once between two sequence points. Read this FAQ on Undefined Behaviour and Sequence Points