Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between static, auto, global and local variable in the context of c and c++

Tags:

c++

c

variables

I’ve a bit confusion about static, auto, global and local variables.

Somewhere I read that a static variable can only be accessed within the function, but they still exist (remain in the memory) after the function returns.

However, I also know that a local variable also does the same, so what is the difference?

like image 364
user1779646 Avatar asked Nov 16 '12 11:11

user1779646


People also ask

What is the difference between static global variable and static local variable in C?

The difference between static global variables and static local variables is that a static global variable can be accessed from anywhere inside the program while a static local variable can be accessed only where its scope exists (i.e block scope).

What is the difference between automatic and static variable in C?

1) A static int variable remains in memory while the program is running. A normal or auto variable is destroyed when a function call where the variable was declared is over. For example, we can use static int to count a number of times a function is called, but an auto variable can't be used for this purpose.

What is the difference between local variable and global variable in C?

The main difference between Global and local variables is that global variables can be accessed globally in the entire program, whereas local variables can be accessed only within the function or block in which they are defined.


1 Answers

There are two separate concepts here:

  • scope, which determines where a name can be accessed, and
  • storage duration, which determines when a variable is created and destroyed.

Local variables (pedantically, variables with block scope) are only accessible within the block of code in which they are declared:

void f() {     int i;     i = 1; // OK: in scope } void g() {     i = 2; // Error: not in scope } 

Global variables (pedantically, variables with file scope (in C) or namespace scope (in C++)) are accessible at any point after their declaration:

int i; void f() {     i = 1; // OK: in scope } void g() {     i = 2; // OK: still in scope } 

(In C++, the situation is more complicated since namespaces can be closed and reopened, and scopes other than the current one can be accessed, and names can also have class scope. But that's getting very off-topic.)

Automatic variables (pedantically, variables with automatic storage duration) are local variables whose lifetime ends when execution leaves their scope, and are recreated when the scope is reentered.

for (int i = 0; i < 5; ++i) {     int n = 0;     printf("%d ", ++n);  // prints 1 1 1 1 1  - the previous value is lost } 

Static variables (pedantically, variables with static storage duration) have a lifetime that lasts until the end of the program. If they are local variables, then their value persists when execution leaves their scope.

for (int i = 0; i < 5; ++i) {     static int n = 0;     printf("%d ", ++n);  // prints 1 2 3 4 5  - the value persists } 

Note that the static keyword has various meanings apart from static storage duration. On a global variable or function, it gives it internal linkage so that it's not accessible from other translation units; on a C++ class member, it means there's one instance per class rather than one per object. Also, in C++ the auto keyword no longer means automatic storage duration; it now means automatic type, deduced from the variable's initialiser.

like image 149
Mike Seymour Avatar answered Oct 14 '22 23:10

Mike Seymour