Variables in c language can have their own attributes
"Attribute" keyword can be added when defining variables
The "attribute" keyword indicates that the variable is meaningful
grammar:
property type var_name
Examples:
int main ()
{
auto char i;
register int j;
static long k;
extern double m;
return 0;
}
auto attribute
auto is the default attribute of local variables in c language
auto indicates that the modified variable is stored on the stack
The compiler defaults all local variables to be auto
Examples:
void f()
{
int i; // The default attribute of local variables is auto
auto int j; // display auto attribute declaration
}
register keyword
The register keyword indicates that local variables are stored in registers
register is just requesting register variables, but not necessarily successful
The register variable must be a value acceptable to the CPU register
Cannot use & operator to get the address of register variable
Global variables cannot be declared as register variables.
#include <stdio.h>
register int g_v; // error
int main ()
{
register char var;
printf("0x%08x",&var);// error
return 0;
}
static keyword
The static keyword indicates the "static" attribute of the variable
Static modified local variables are stored in the static area of the program
The static keyword also has the meaning of "scope qualifier"
Static modified global variable scope is only in the declared file
Static modified function scope is only in the declared file
#include<stdio.h>
int g_v; // Global variable program can be accessed anywhere
static int g_v; // Static global variable, accessible only in the current file
int main ()
{
int var; // local variable, allocate space on the stack
static int svar; // Static local variable, allocate space in static data area
return 0;
}
extern keyword
extern is used to declare "external" defined variables and functions
The extern variable allocates space elsewhere in the file
The extern function is defined elsewhere in the file
extern is used to "tell" the compiler to compile in C mode
The c ++ compiler and some variants of the c compiler default will compile functions and variables in the "own" way, and the compiler can be named "compiled in the standard c way" through the extern key
external “c”
{
int f(int a, int b)
{
return a+b;
}
}
}
summary:
The auto variable is stored on the stack of the program, the default attribute
static variables are stored in the static area of the program
Register variable request is stored in CPU register
The extern variable allocates space elsewhere in the file
extern can instruct the compiler to compile the program according to the standard c way