Confusion assignment operations

What is the result of the following code:

int main() {
  int k = (k = 2) + (k = 3) + (k = 5);
  printf("%d", k);
}

It makes no mistake, why? I think this should give an error because the assignment operations are on the same line as the definition k.

What I mean int i = i;cannot compile. But it compiles. What for? What will be the result and why?

+3
source share
3 answers

int i = i compiles because 3.3.1 / 1 (C ++ 03) says

The declaration point for the name immediately after its full declaration and before its initializer

So, iinitialized with its own undefined value.

Undefined , k . FAQ Undefined

+7

int = i; , . C . , , .

C . "10", "k" "a".

0

, 11. , k 3 , 5 . int k = (k=2)+(k=3) 6, int k = (k=2)+(k=4) 8, int k = (k=2)+(k=4)+(k=5) 13. int k = (k=2)+(k=4)+(k=5)+(k=6) 19 (4 + 4 + 5 + 6).

? . (k = x) , . , k + k, k, , ( k). , ( k ). , k ( ) k .

0

All Articles