Home Backend Development C#.Net Tutorial How to use static in c language

How to use static in c language

May 09, 2024 am 10:48 AM
c language Scope

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

How to use static in c language

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.

staticScope of variables

For variables, static means:

  • Internal linking: staticA variable can only be accessed within the file in which it is declared.
  • Retained value: staticA 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 local static variables are initialized to garbage values.

The static scope of a function

For a function, static means:

  • Internal linkage: staticA function can only be called within the file in which it is declared.
  • Local scope: staticLocal variables of a function have the same internal scope and value retention properties as static variables.

staticscope of class members

For class members, static means:

  • Class scope: staticClass members belong to the entire class, not to any specific object.
  • Shared data: All class instances share the same static class members.
  • Constant: static constClass 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;
};
Copy after login

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!

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

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)

Usage of typedef struct in c language Usage of typedef struct in c language May 09, 2024 am 10:15 AM

Usage of typedef struct in c language

What does real mean in c language What does real mean in c language May 09, 2024 pm 12:06 PM

What does real mean in c language

Advantages and disadvantages of closures in js Advantages and disadvantages of closures in js May 10, 2024 am 04:39 AM

Advantages and disadvantages of closures in js

What does include mean in c++ What does include mean in c++ May 09, 2024 am 01:45 AM

What does include mean in c++

How to implement the power function in C language How to implement the power function in C language May 09, 2024 pm 11:33 PM

How to implement the power function in C language

What to do if there is an error in scanf in C language What to do if there is an error in scanf in C language May 09, 2024 am 11:39 AM

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

What does reg mean in c language What does reg mean in c language May 09, 2024 am 09:57 AM

What does reg mean in c language

C++ smart pointers: a comprehensive analysis of their life cycle C++ smart pointers: a comprehensive analysis of their life cycle May 09, 2024 am 11:06 AM

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

See all articles