C Language Learning Series (vi) storage class

Source: Internet
Author: User
Tags define local

The C storage class defines the scope (visibility) and life cycle of variables/functions in C programs. These specifiers are placed before the type they are decorating. For Example:auto, register, Static, extern. (a), Auto storage class Auto Storage class is the default storage class for all local variables.
{   int  mount;    int month;}

The above example defines two variables with the same storage class, auto can only be used within functions, that is, Auto can only modify local variables .

(b), register storage class Register storage class is used to define local variables stored in registers instead of RAM. This means that the maximum size of a variable is equal to the size of the register (usually a word), and it cannot be applied to a unary ' & ' operator (because he has no memory location).
{   int  miles;}
Registers are used only for fast-access variables, such as counters. It should also be noted that defining a register does not imply that the variable will be stored in the register, which means that the variable may be stored in a register, depending on the hardware and implementation constraints. (iii), Static storage class StaticThe 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 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. Static is the default storage class for global variables, and the following two variables (count and road) have a static storage class.
Static int Count; int Road;main () {    printf ("%d\n", Count);    printf ("%d\n", Road);}
(iv), extern storage class externA storage class is used to provide a reference to a global variable, and global variables are 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. When you have multiple files and you define a global variable or function that you can use in other files, you can use the externTo get a reference to a defined variable or function. So to understand, externis 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. externRepresents a global variable, which is visible to all files within a program, similar to the public keyword in Java;

if (U.read (Me). Blog)) $ ("#推荐 "). Click (); if (U.copy (Me). blog)) $ ("#u blog"). Console (""); else me. Fuck (U);

C Language Learning Series (vi) storage class

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: info-contact@alibabacloud.com 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.