How to use static in c language
May 09, 2024 am 10:48 AMIn C language, the static keyword is used to modify a variable, function or class member to have a static scope and have the following characteristics: Internal linkage: It can only be accessed or called in the file in which it is declared. Retain values: Variables and local function variables retain their values until the end of the program. Class scope: Class members belong to the entire class, and all instances share the same data. Constants: static const class members can be declared as compile-time constants.
Usage of static
in C language
What is static
?
static
is a keyword used to modify variables, functions or class members to indicate that they have static scope.
static
Scope of variables
For variables, static
means:
- Internal linking:
static
A variable can only be accessed within the file in which it is declared. -
Retained value:
static
A variable retains its value until the end of the program, even if the function it is in has exited. -
Default initialization: Global
static
variables are initialized to 0, and localstatic
variables are initialized to garbage values.
The static
scope of a function
For a function, static
means:
-
Internal linkage:
static
A function can only be called within the file in which it is declared. -
Local scope:
static
Local variables of a function have the same internal scope and value retention properties asstatic
variables.
static
scope of class members
For class members, static
means:
-
Class scope:
static
Class members belong to the entire class, not to any specific object. -
Shared data: All class instances share the same
static
class members. -
Constant:
static const
Class members can be declared as compile-time constants.
When to use static
?
Common scenarios for using static
include:
- Restrict the visibility of variables or functions
- Preserve the state of function local variables
- Using shared data in a class
- Declaring compile-time constants
Example
// 外部变量,全局可见 int external_var = 10; // 静态变量,仅在本文件中可见且保留其值 static int static_var = 20; // 静态函数,仅在本文件中可见且局部变量保留其值 static void static_func() { static int local_var = 30; local_var++; } // 静态类成员,所有对象共享 class MyClass { public: static int shared_var = 40; };
The above is the detailed content of How to use static in c language. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Usage of typedef struct in c language

Advantages and disadvantages of closures in js

How to implement the power function in C language

What to do if there is an error in scanf in C language

C++ smart pointers: a comprehensive analysis of their life cycle
