search
HomeCommon ProblemWhat is the use of static variables in C language

What is the use of static variables in C language

May 05, 2019 pm 04:42 PM
staticstatic variable

The role of static variables in C language is to keep the value unchanged during the function call, because static variables have a memory function; but their scope is limited and can only be accessed by functions within the module, etc. .

What is the use of static variables in C language

Recommended: "c Tutorial"

The role of static variables in C language is: static variables It has a memory function and keeps the value unchanged during the function call; the scope is limited and can only be accessed by functions within the module, etc.

What is the use of static variables in C language

In C language, the keyword static means static and has three obvious functions:

1. In the function body, static variables It has a memory effect, that is, the value of a variable declared as static remains unchanged during the process of this function being called.

2. Within a module (but outside a function), its scope is limited. If a variable is declared static, then the variable can be accessed by all functions in the module, but It cannot be accessed by other functions outside the module.

3. Internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, they should be described in a header file. The source files that use these functions must include this header file. .

Characteristics of static data members:

1. For non-static data members, each class has its own copy. Static data members are treated as members of the class. No matter how many objects of this class are defined, there is only one copy of the static data members in the program, which is shared and accessed by all objects of this type.

2. Static data members are stored in the global data area. Space must be allocated when defining, so it cannot be defined in the class declaration. Since static data members are shared by all objects of this class, they do not belong to a special class object. When no class object is generated, its scope is visible, that is, programmers can also use it when no instance of the class is generated.

3. Static data members also comply with public, protect, and private access rules.

4. The initialization of static member variables is outside the class, and the static keyword cannot be used at this time. Although the static members of private and protect can be initialized outside the class, they cannot be accessed outside the class.

Advantages of static data members:

1. Static data members do not enter the global name space of the program, so there is no possibility of conflict with other global names in the program. .

2. Information hiding can be achieved. Static data members can be private members, but global variables cannot.

#include<stdio.h>
#include <iostream>
#include "a.h"
using namespace std;
void fun(int i)
{    static int value = i++;
    cout<<value<<endl;
}int main()
{
    fun(0);
    fun(1);
    fun(2);    return 0;
}

Running result

0
0
0

The above is the detailed content of What is the use of static variables in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.