C Local variables of a function exist during function execution and are destroyed when the function returns. Their scope is limited to the function and cannot be accessed outside the function.
The lifetime of local variables of C function
In C, local variables are variables declared and initialized inside the function . Their scope is limited to that function, meaning they can only be accessed and modified during function execution.
The lifetime of local variables:
Practical case:
Let us consider the following C function:
void printNumbers() { int number = 10; // 局部变量 cout << "Number: " << number << endl; }
When the printNumbers()
function is When called, the local variable number
will be created and initialized to 10. This variable will exist during the execution of the function, that is, after the statement where it prints "Number: 10". When the function returns, number
is destroyed so it cannot be accessed outside the function.
Points:
The above is the detailed content of What is the lifetime of local variables of a C++ function?. For more information, please follow other related articles on the PHP Chinese website!