Two properties of data in C language and the function of static

Source: Internet
Author: User

There are two types of data in the C language: data types and Storage categories. The data type defines the data format (length), and the storage category defines the scope and lifetime of the data.

1. Declaration of variables

1.1

The general form of the declaration of a variable: stores the class data type variable name, and the data type takes int as an example:

  • Auto variable: Auto int i;//automatic variable is local variable
  • Local variables: variables defined inside a function, local variables default storage categories are automatic variables
    • Scope: From definition start to function end.

    • Lifetime: From the beginning of the function call until the function is launched.

  • external variable/global variable: A variable that is defined outside the function and does not indicate a storage class
    • defines an external variable: extern int i = 1; or int i;

    • Declare an external variable: extern int i;
    • scope: From the definition start to the end of the source file.
      • : When a C program consists of multiple source files, The external variable i is defined in a source file, the other source files to use I need to declare the I,extern int i in the source file;. A static global variable can only be used in a defined source file and cannot be used by another source file.
    • life: From the start of the definition to the end of program execution.
    • Expand external variable scope with extern declaration external variable
  • Static variables: static int i; register variable: rigister int i;
    • Static local Variables
      • Scope: From definition start to function end.
      • lifetime: From the definition start to the end of the program execution .
    • Static global variables
      • Scope: From the definition start to the end of the source file. That is, the extern declaration cannot be used by other source files.
      • Lifetime: From the definition start to the end of the program execution.
  • Register variables: rigister int i; Register variables are stored in the CPU.

1.2

    • Static allocation: Static storage of data, including static variables and external variables, to release the memory units occupied by the variables at the end of the program execution.
    • Dynamic allocation: There is a dynamic store of data, including register variables and local variables, and the memory units occupied by the variables are released when the function is executed.

The role of 2.static

2.1static Acting on variables

    • Change scope (static global variable)
    • Change lifetime (static local variable)

2.2static Acting on functions

    • Change function Scope: a function that is modified by static is called an intrinsic function and can only be used in its defined source file. The default value for a function that is not modified by static is extern, which is an external function that can be used by the program's odd Tan Yuan file.

Static and unnamed namespace in the 3.c++

Unnamed namespace is superior to static keyword, primarily because the keyword applies only to the static variables declarations and functions, not to the user-defined types.

Two properties of data in C language and the function of static

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.