The role of static in C language

Source: Internet
Author: User
Tags modifier

Reprinted from 52725595

The role of static in the C language is as follows

first , when modifying a variable, static local variables are executed only once, and the life cycle of the local variable is extended, until the end of the program runs.
second , when static modifies global variables, this global variable can only be accessed in this file and cannot be accessed in other files, even extern external declarations.
third , static modifies a function, the function can only be called in this file, cannot be called by other files. Static modified local variables are stored in the global data area of the rest of the variable area. Initialization is automatically initialized to 0;
(1) When you do not want to be released, you can use the static modifier. For example, an array of modifier functions stored in the stack space. You can use the static modifier if you do not want this array to be released at the end of a function call
(2) Take into account the data security (when the process wants to use the global variable should consider the use of static)

The static keyword in C + + has a role in C and is used in a class
In a class, static can be used to decorate static data members and static member methods
static data members
(1) A static data member can implement data sharing between multiple objects, which is a shared member of all objects of the class, it occupies only one portion of the memory, and if it changes its value, the value of the data member in each object is changed.
(2) A static data member is allocated space when the program starts running and is released after the program ends, as long as a static data member is specified in the class, and the static data member is allocated space even if the object is not defined.
(3) Static data members can be initialized, but can only be initialized outside the class body, if the static data members are assigned the initial value, the compiler will automatically initialize it to 0
(4) A static data member can be referenced either by an object name or by a class name.

Static member functions
(1) Static member functions, like static data members, belong to static members of the class, not to object members.
(2) A non-static member function has this pointer, whereas a static member function does not have the this pointer.
(3) Static member functions are primarily used to position static data members and cannot access non-static members.

The role of static 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: 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.