Use of static in C language

Source: Internet
Author: User

During the development process. We often need to define variables or functions that are static types. Let's talk about static in detail next.

1. Modifier variables

When static modifies a variable, it is doomed to the visible range and life cycle of the variable;

(1) When modifying global variables

static int flag1 = 0;

int flag2 = 0;

These two variables are stored in the global data area. Flag1 is only visible in this document. Not visible in other files, Flag2 can be used in other files by declaring extern int flag2;

(2) When modifying local variables

void Fun (void) {

static int temp1;

int temp2 = 0;

......................

Return

}

function. Temp1 is a local static variable, stored in the global data area and TEMP2 as a local variable. Stored on the stack, but as the function exits. The life cycle of the TEMP2 is also over. But Temp1 still works. Only the visible range is within this function. The next time you enter this function again, whatever changes you make to TEMP1 are made on the basis of the last change. That is to say, Temp1 has memory.

2. Modifier function

The function of the static modifier is primarily the function used in this file. Not provided correctly, this type of static function is available in any file in the Linux kernel.

static inline void Enable_noirq (void) {

................

}

The advantage of using the static modifier function is. All files can define a function with the same name, without considering the compilation error caused by the duplicate names;

Use of static in C language

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.