Home Common Problem What 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
static static 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;
}
Copy after login

Running result

0
0
0
Copy after login

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 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

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
What is the static variable mechanism of PHP functions? What is the static variable mechanism of PHP functions? Apr 10, 2024 pm 09:09 PM

The static variable mechanism of PHP functions allows variables to retain their values ​​between function calls, thus achieving the following functionality: Preserving state between function calls. Avoid creating duplicate variables. Simplify the code.

What is the function and usage of static in C language What is the function and usage of static in C language Jan 31, 2024 pm 01:59 PM

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

Practical application scenarios and usage skills of the static keyword in C language Practical application scenarios and usage skills of the static keyword in C language Feb 21, 2024 pm 07:21 PM

Practical application scenarios and usage skills of the static keyword in C language 1. Overview static is a keyword in C language, used to modify 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 actual application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples. 2. Static variables extend the life cycle of variables. Using the static keyword to modify local variables can extend their life cycle.

How to use static, this, super, and final in Java How to use static, this, super, and final in Java Apr 18, 2023 pm 03:40 PM

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.

How to use Java modifiers abstract, static and final How to use Java modifiers abstract, static and final Apr 26, 2023 am 09:46 AM

Modifier abstract (abstract) 1. Abstract can modify a class (1) The class modified by abstract is called an abstract class (2) Syntax: abstractclass class name {} (3) Features: Abstract classes cannot create objects separately, but they can be declared Reference the abstract class name reference name; (4) Abstract classes can define member variables and member methods (5) Abstract classes have constructors. When used to create subclass objects, jvm creates a parent class object by default; abstract constructor methods apply Applied when jvm creates parent class object. 2. Abstract can modify methods (1) The method modified by asbtract is called an abstract method (2) Syntax: access modifier abstract return value

The role of static The role of static Jan 24, 2024 pm 04:08 PM

The functions of static: 1. Variables; 2. Methods; 3. Classes; 4. Other uses; 5. Multi-threaded environment; 6. Performance optimization; 7. Singleton mode; 8. Constants; 9. Local variables; 10. Memory Layout optimization; 11. Avoid repeated initialization; 12. Use in functions. Detailed introduction: 1. Variables, static variables. When a variable is declared as static, it belongs to the class level, not the instance level, which means that no matter how many objects are created, only one static variable exists, and all objects share this Static variables and so on.

How Springboot reads custom pro files and injects static variables How Springboot reads custom pro files and injects static variables May 30, 2023 am 09:07 AM

Springboot reads the pro file and injects static static variables mailConfig.properties#Server mail.host=smtp.qq.com#Port number mail.port=587#Email account mail.userName=hzy_daybreak_lc@foxmail.com#Email authorization code mail.passWord =vxbkycyjkceocbdc#Time delay mail.timeout=25000#Sender mail.emailForm=hzy_daybreak_lc@foxmail.com#Sender mai

What is the static method of php What is the static method of php Oct 31, 2022 am 09:40 AM

The "static" in php static static methods means that these properties and methods can be called directly without instantiating the class; static is a keyword used to modify the properties and methods of the class, and its usage syntax is such as "class Foo {public static $my_static = 'hello';}".