What does printf print for a uniform variable?

What should the code print? 0 or any garbage value or will it depend on the compiler?

#include <stdio.h>
int a;
int main() 
{ 
   printf("%d\n",a);
   return 0;
}
-1
source share
5 answers

the answer is 0. Global variables are initialized to zero.

+9
source

I would say that your code can output anything or just something can happen, because your code causes Undefined Behavior according to C99.

You do not have a prototype for printfin scope.

J.2 Undefined Behavior

- , , , ( 6.5.2.2).

, a 0, .

+4

C99, 6.7.8.10, :

, . , , , : - , ; - , ( ) ; - , () ; - , () .

6.2.4.3 :

, , . - , .

, 0. (.. -t20) ) .

+2

[ , ] 0

0

0. (.. ) .

0

Source: https://habr.com/ru/post/1524084/


All Articles