Local, Global, Extern and Static Variables in C

Variables are an essential part of programming languages that are used to store data in memory for further processing. In C, variables are broadly categorized into three types: static, local, global, and extern. Each of these variable types has a different scope and lifetime, and understanding their differences is crucial for writing efficient and bug-free code.

In this blog, we will discuss static, local, global, and extern variables in C, along with examples.

Static Variables

Static variables are declared with the keyword static. They are created and initialized only once, and their value persists between function calls. Static variables have a local scope, which means they can only be accessed within the function where they are declared. However, their lifetime is the entire program, which means that they retain their value even after the function execution is complete. Static variables are useful in cases where we need to store data between function calls.

#include <stdio.h>

void printStatic() {
   static int count = 0;
   count++;
   printf("Static count: %d\n", count);
}

int main() {
   printStatic();
   printStatic();
   printStatic();
   return 0;
}

Output:

Static count: 1
Static count: 2
Static count: 3

In the above example, the count variable is declared as static inside the printStatic() function. Each time the function is called, the count variable is incremented by 1, and its value persists between function calls.

Local Variables

Local variables are declared inside a function or a block of code and have a limited scope. They are destroyed when the function or block of code is exited. Local variables are useful when we need to store temporary data that is required only for a short time.

#include <stdio.h>

void printLocal() {
   int count = 0;
   count++;
   printf("Local count: %d\n", count);
}

int main() {
   printLocal();
   printLocal();
   printLocal();
   return 0;
}

Output:

Local count: 1
Local count: 1
Local count: 1

In the above example, the count variable is declared as local inside the printLocal() function. Each time the function is called, a new count variable is created, initialized to 0, and then incremented by 1. Since the count variable is local to the function, its value is destroyed when the function is exited, and a new count variable is created when the function is called again.

Global Variables

Global variables are declared outside of any function or block of code and have a global scope. They can be accessed from any function or block of code within the program. Global variables have a lifetime that is the entire program, which means that their value persists throughout the program’s execution. Global variables are useful when we need to share data between different functions or blocks of code.

#include <stdio.h>

int count = 0;

void printGlobal() {
   count++;
   printf("Global count: %d\n", count);
}

int main() {
   printGlobal();
   printGlobal();
   printGlobal();
   return 0;
}

Output:

Global count: 1
Global count: 2
Global count: 3

In the above example, the count variable is declared as global outside of any function. Each time the printGlobal() function is called, the count variable is incremented by 1, and its value persists throughout the program’s execution.

Extern Variables

Extern variables are variables that are declared in one file but are used in another file. They are declared in the file where they are defined using the keyword extern. Extern variables are typically used in larger programs where multiple files are used.

Here’s an example of how to use extern variables:

file1.c:

#include <stdio.h>

int count;

void demo() {
    count++;
}

In this file, we declare a global variable count without initializing it.

file2.c:

#include <stdio.h>

extern int count;

int main() {
    printf("count = %d\n", count); // Output: count = 0
    demo();
    printf("count = %d\n", count); // Output: count = 1
    demo();
    printf("count = %d\n", count); // Output: count = 2
    return 0;
}

In this file, we use the extern keyword to indicate that count is defined in another file. We then print the value of count, which is initialized to 0. We call the demo() function, which increments the value of count. We then print the value of count again, which is now 1. We call the demo() function again and print the value of count, which is now 2.

Note that in order for the extern keyword to work correctly, the variable must be declared in one file and defined in another file. In this example, count is declared in file1.c and defined (initialized) in file2.c.

In conclusion, understanding the differences between static, local, global, and extern variables is crucial for writing

Leave a Comment

Your email address will not be published. Required fields are marked *