English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Las variables globales y estáticas se inicializan con su valor predeterminado porque se encuentra en C o C ++En el estándar, y se puede asignar libremente a cero en tiempo de compilación. El comportamiento de las variables estáticas y globales es el mismo que el del código objetivo generado. Estas variables se asignan en el archivo .bss y se asigna memoria al cargar, obteniendo la constante asignada a la variable.
A continuación, se muestra un ejemplo de variables globales y estáticas.
#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; }
Resultados de salida
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
En el programa anterior, las variables globales estánmain()
Se declaran fuera de la función, y una de ellas es una variable estática. Se declaran tres variables locales, y la variable z también se inicializa.
int a; static int b; ... int x; static int y; int z = 28;
Se imprimirá su valor predeterminado.
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);