Home > Backend Development > C++ > body text

Practical application scenarios and usage skills of the static keyword in C language

王林
Release: 2024-02-21 19:21:03
Original
711 people have browsed it

Practical application scenarios and usage skills of the static keyword in C language

Practical application scenarios and usage skills of static keyword in C language

1. Overview
static is a keyword in C language, used for modification variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the practical application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples.

2. Static variables

  1. Extension of the life cycle of variables
    Using the static keyword to modify local variables can extend their life cycle to the entire running process of the program. This means that the value of the variable remains unchanged even if it leaves the scope in which it resides. This feature is very useful in scenarios where the state of a variable needs to be maintained. For example, in a recursive function, we can use static variables to record the number of times the function is called.
#include <stdio.h>

int recursive()
{
    static int count = 0;
    count++;

    printf("当前递归次数:%d
", count);

    if (count < 5)
    {
        recursive();
    }

    return count;
}

int main()
{
    int result = recursive();

    printf("递归结束,共计调用次数:%d
", result);

    return 0;
}
Copy after login

Running results:

当前递归次数:1
当前递归次数:2
当前递归次数:3
当前递归次数:4
当前递归次数:5
递归结束,共计调用次数:5
Copy after login

It can be seen that by using the static keyword to modify the count variable, the value of the variable is maintained during the recursive call, achieving the accumulation of the number of recursions. .

  1. Control the visibility of variables
    Using the static keyword to modify a global variable can limit its scope to the current source file and avoid being accessed in other source files. In this way, we can define static variables with the same name in different source files without conflict problems. This feature is very useful in scenarios where you need to share variables while ensuring the closure of the variable scope.
// file1.c
#include <stdio.h>

static int global = 10;

void printGlobal()
{
    printf("file1.c中的global:%d
", global);
}
Copy after login
// file2.c
#include <stdio.h>

static int global = 20;

void printGlobal()
{
    printf("file2.c中的global:%d
", global);
}
Copy after login
// main.c
#include <stdio.h>

extern void printGlobal();

int main()
{
    printGlobal();

    return 0;
}
Copy after login

Run result:

file1.c中的global:10
Copy after login

In this example, because the global variable is modified by the static keyword, static variables with the same name can be defined in different source files without Cause conflict.

3. Static function

  1. Control the visibility of the function
    Using the static keyword to modify the function can limit its scope to the current source file and avoid using it in other source files. is called in. In this way, we can define static functions with the same name in different source files without conflict problems. This feature is very useful in scenarios where you need to encapsulate function implementation without exposing it to other modules.
// file1.c
#include <stdio.h>

static void privateFunc()
{
    printf("这是file1.c中的私有函数
");
}

void publicFunc()
{
    printf("这是file1.c中的公共函数
");
    privateFunc();
}
Copy after login
// file2.c
#include <stdio.h>

static void privateFunc()
{
    printf("这是file2.c中的私有函数
");
}

void publicFunc()
{
    printf("这是file2.c中的公共函数
");
    privateFunc();
}
Copy after login
// main.c
#include <stdio.h>

extern void publicFunc();

int main()
{
    publicFunc();

    return 0;
}
Copy after login

Running results:

这是file1.c中的公共函数
这是file1.c中的私有函数
Copy after login

In this example, since the privateFunc function is modified by the static keyword, static functions with the same name can be defined in different source files without Cause conflict.

  1. The function is only initialized once
    Using the static keyword to modify a local variable can cause the variable to be initialized only once and keep its value unchanged between multiple calls to the function. This feature is very useful in scenarios where the state of a certain variable needs to be recorded. For example, in a function you need to record the number of function calls.
#include <stdio.h>

void printCount()
{
    static int count = 0;
    count++;

    printf("函数调用次数:%d
", count);
}

int main()
{
    int i;
    for (i = 0; i < 5; i++)
    {
        printCount();
    }

    return 0;
}
Copy after login

Running results:

函数调用次数:1
函数调用次数:2
函数调用次数:3
函数调用次数:4
函数调用次数:5
Copy after login

You can see that by using the static keyword to modify the count variable, the value of the variable is maintained between multiple calls of the function, realizing the function The cumulative number of calls.

4. Summary
This article introduces the practical application scenarios and usage techniques of the static keyword in C language. By describing the examples of static variables and static functions in detail, we can find that the static keyword plays an important role in extending the life cycle of variables, controlling the visibility of variables and functions, and controlling the number of variable initializations. Reasonable application of the static keyword can improve the readability, maintainability and security of the program. I hope this article will be helpful to readers in their application of C language programming.

The above is the detailed content of Practical application scenarios and usage skills of the static keyword in C language. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template