Zasto ovo ne radi?

Zasto ovo radi: https://i.imgur.com/N1A8iyt.png
A ovo ne: https://i.imgur.com/Mfx2UeZ.png
(Pola sata pokusavam da nadjem gresku i nista, a u codeblox mi pokazuje tacne rezultate i za 1 i za 2.)

Mislim da int deklarisan van int main-a ima automatski vrednost stavljenu na 0, zato u 2. primeru treba staviti int counter = 0;

1 Like

Deklarisano van funkcije: ima vrednost 0 (ili null). Deklarisano unutar funkcije: inicijalna vrednost nije definisana, što znači da na tvojoj mašini u nekom trenutku (ili stalno) može biti 0, a ne mora.

Konkretno, u standardu ima odeljak 6.7.10 / Semantics koji sadrži stavku:

If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. If an object that has static or thread storage duration is not initialized
explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules,
and any padding is initialized to zero bits;
— if it is a union, the first named member is initialized (recursively) according to these
rules, and any padding is initialized to zero bits;