English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Global variables and static variables are initialized to their default values because they are in C or C ++In the standard, and it can be freely assigned to zero at compile time. The behavior of static variables and global variables is the same as the generated target code. These variables are allocated in the .bss file, and memory is allocated by getting the constant assigned to the variable when it is loaded.
The following is an example of global and static variables.
#include <stdio.h> int a; static int b; int main() { int x; static int y; int z = 28; printf("The default value of global variable a: %d", a); printf("\nThe default value of global static variable b: %d", b); printf("\nThe default value of local variable x: %d", x); printf("\nThe default value of local static variable y: %d", y); printf("\nThe value of local variable z: %d", z); return 0; }
Output result
The default value of global variable a: 0 The default value of global static variable b: 0 The default value of local variable x: 0 The default value of local static variable y: 0 The value of local variable z: 28
In the above program, global variables aremain()
Declared outside the function, and one of them is a static variable. Three local variables are declared, and the variable z is also initialized.
int a; static int b; ... int x; static int y; int z = 28;
It will print its default value.
printf("The default value of global variable a: %d", a); printf("\nThe default value of global static variable b: %d", b); printf("\nThe default value of local variable x: %d", x); printf("\nThe default value of local static variable y: %d", y); printf("\nThe value of local variable z: %d", z);