C language variables and function types

Source: Internet
Author: User

Extern and static can be used to modify variables and functions. What is the difference and usage.

1. Variables

By storage type, there are four types of variables in C language:

The auto keyword, which can be omitted. For example, int I; variable I is the auto variable.

The external variable keyword extern is called an external variable because it must be used externally, that is, in other files. Global variables are generally stored using external variables. If an external variable is defined before, after, or referenced in another source file, it must be declared using extern.

Static variables include static global variables and static local variables. Static global variables can only be used in the current source file. The scope of a static local variable is the function body that calls the variable, but it occupies the allocated memory from start to end throughout the entire running process of the program.

The register variable is stored in the CPU register. Since the compiler has optimized the variable, this type is basically not used.

For local variables, the global variables and local variables can have the same name in the same original file. In the scope of local variables, global variables do not work.

2. Functions

Functions are divided into external and internal functions.

External function: A function can be used by other source files to define an external function. Add the keyword "extern" before the function, which is usually omitted. The default function is an external function. ,

Internal function: A function can only be called by other functions in this file. When defining an internal function, add the keyword static before the function.

 

The reason why so many types need to be created is to meet different functional needs, while enhancing the readability, independence and portability of source files.

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.