Home > Backend Development > C++ > What is the difference between local variables and global variables of a C++ function?

What is the difference between local variables and global variables of a C++ function?

WBOY
Release: 2024-04-19 15:42:01
Original
445 people have browsed it

C The difference between local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

C++ 函数的局部变量和全局变量有什么区别?

Local variables and global variables of C functions: big difference

In C, there are important differences between local variables and global variables. Understand these The distinction is critical to writing efficient, maintainable code.

Local variables

  • are defined within a function and are only visible within the function scope.
  • Created when the function is called and destroyed when the function returns.
  • Local variables will be recreated every time the function is called.

Sample code:

void myFunction() {
    int localVariable = 5; // 局部变量
    // ... 使用 localVariable
}

int main() {
    myFunction();
    // localVariable 无法访问,因为它不在 main() 函数的范围内
}
Copy after login

Global variables

  • are defined outside the function and are visible throughout the entire program.
  • Created when the program starts and destroyed when the program terminates.
  • Global variables are visible to all functions in the program.

Sample code:

int globalVariable = 10; // 全局变量

void myFunction() {
    // ... 使用 globalVariable
}

int main() {
    // ... 使用 globalVariable
}
Copy after login

Difference

FeaturesLocal variables Global variables
VisibilityLimited to the function in which they are definedEntire program
Life cycleDuring function callDuring program running
Memory allocation On the stackIn the global data area
ScopeIn the functionIn the entire program
InitializationWhen the function is calledWhen the program starts
RecreateEvery time the function When calledOnly when the program starts

Actual case

Examples of local variables

In the following example , the local variable name is only used within the greet() function, and is recreated each time the function is called:

void greet(std::string name) {
    std::cout << "Hello, " << name << "!" << std::endl;
}

int main() {
    greet("John");
    greet("Mary"); // 局部变量 name 将重新创建
}
Copy after login

Example of global variables

In the following example, the global variable g_count is visible program-wide and is updated every time the function is called:

int g_count = 0; // 全局变量

void incrementCount() {
    g_count++;
}

int main() {
    incrementCount();
    std::cout << "Count: " << g_count << std::endl; // 输出 1

    incrementCount();
    std::cout << "Count: " << g_count << std::endl; // 输出 2
}
Copy after login

The above is the detailed content of What is the difference between local variables and global variables of a C++ function?. 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