The role of keywords auto, static, register, const, volatile, extern in C language

Source: Internet
Author: User

This keyword is used to declare that the lifetime of a variable is automatic, and that variables defined in any class, struct, enumeration, union, and function are not treated as global variables, whereas variables defined in a function are considered local variables . This keyword doesn't write much, because all variables are auto by default.


intfunc () {AutointA; return 1;} Autointb = A;intMainintargcChar Const*argv[]) {printf ("%d\n", B); return 0;}/*Mainc.c:7:10:error:file-scope declaration of ' B ' specifies ' auto ' auto int b = $;*/


This keyword command compiler as far as possible to have variables in the CPU internal registers rather than through memory addressing access to improve efficiency.

intMainintargcChar Const*argv[]) {Registerintb =Ten; printf ("%d\n", B); printf ("%p\n", &b); return 0;}/*mainc.c:in function ' main ': mainc.c:13:2: error:address of register variable ' B ' requested printf ("%p\n", &b); 


The 3.1 static storage class instructs the compiler to maintain the existence of local variables for the lifetime of the program without having to create and destroy each time it enters and leaves the scope. Therefore, using the static modifier local variable can maintain the value of a local variable between function calls.

The 3.2 static modifier can also be applied to global variables. When static modifies a global variable, the scope of the variable is limited to the file in which it is declared.

3.3 Static is the default storage class for global variables


The 4.1 extern storage class is used to provide a reference to a global variable that is visible to all program files. When you use ' extern ', the variable name is pointed to a previously defined storage location for variables that cannot be initialized.

4.2 When you have multiple files and you define a global variable or function that you can use in other files, you can use extern in other files to get a reference to a defined variable or function. As you can understand,extern is used to declare a global variable or function in another file.

The extern modifier is typically used when two or more files share the same global variable or function

int count; extern void int main () { 5; extern int void write_extern (void) { printf ("count is%d\n ", Count);}



The role of keywords auto, static, register, const, volatile, extern in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.