Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - What is the default value of int variables defined in C language?
What is the default value of int variables defined in C language?
If it is a defined global variable or static variable, it is 0 when it is not initialized. If it is a local variable, it is a random value left on the stack before.

There are two ways to distinguish the types of variables.

1, local variable.

When the local variable is not explicitly initialized, its value is not required by the C language specification, and it can be a random value or a value arbitrarily given by the compiler.

For example, the local variable of gcc compiler is a random value, which can be any value. Microsoft compilers, such as VC or VS, will be initialized to full C, that is, 0xCCCCCCCC.

2. Global variables or static local variables.

All global variables, that is, variables defined outside the function, have a default value of 0.

All static local variables are defined in the form of static int name inside the function, and initialized to 0 by default.

Extended data:

Int is a data type. Use programming languages (C, C++, C#, Java, etc. ), which is an identifier used to define an integer variable.

At present, in general-purpose computers, int occupies 4 bytes and 32 bits, and the data range is-2147483648 ~ 2147483647 [-231~ 231].

In the previous microcomputer, int occupied 2 bytes, 16 bits, and the data range was-32768 ~ 32767 [-215 ~ 215-1].

Definition method:

int? Me; //define the integer variable i.

int? I = 1; //Define the integer variable I, and initialize it to 1.

Define constants:

const? int? Me; //Compilation error, because constants need to be given initial values.

const? int? I = 1; //Define the constant i with the value of 1.

Constants defined by Java:

Final? int? I = 1; //Define the constant i with the value of 1.

Baidu Encyclopedia -INT (data type)