Unit-II-Variable Scope and Storage Classes
Unit-II-Variable Scope and Storage Classes
Variable Scope
In C programming, variable scope refers to the region of the code where a variable is
accessible and can be used.
Here's a breakdown of variable scope in C, along with examples:
1. Local Scope: Variables declared within a function or block are accessible only within
that function or block.
Example:
#include <stdio.h>
void myFunction()
{
int x = 10; // Local variable
printf("%d\n", x);
}
int main()
{
// x is not accessible here
printf("Hello, World!\n");
myFunction();
return 0;
}
2. Global Scope: Variables declared outside all functions and blocks are accessible from
any part of the program.
Example:
#include <stdio.h>
int x = 10; // Global variable
void myFunction()
{
printf("Global x: %d\n", x);
}
int main()
{
printf("Global x: %d\n", x);
myFunction();
return 0;
}
1
Unit-II Functions and Arrays
Variable Scope – Storage Classes
3. Block Scope: Variables declared within a block (e.g., if, while, for) are accessible only
within that block.
Example:
#include <stdio.h>
int main()
{
int i = 5;
if (i > 10) {
int x = 20; // Block variable
printf("Block x: %d\n", x);
}
// x is not accessible here
printf("Hello, World!\n");
return 0;
}
4. Function Parameter Scope: Function parameters are accessible only within the
function.
Example:
#include <stdio.h>
5. Static Scope: Variables declared with the static keyword retain their value between
function calls.
Example:
#include <stdio.h>
void myFunction()
{
static int x = 10; // Static variable
printf("Static x: %d\n", x);
x++;
}
2
Unit-II Functions and Arrays
Variable Scope – Storage Classes
int main()
{
myFunction(); // Prints 10
myFunction(); // Prints 11
return 0;
}
When multiple variables with the same name are declared in different scopes, the
innermost scope takes precedence.
Example:
#include <stdio.h>
void myFunction()
{
int x = 20; // Local variable
printf("Local x: %d\n", x);
}
int main() {
printf("Global x: %d\n", x);
myFunction();
return 0;
}
Output:
Global x: 10
Local x: 20
3
Unit-II Functions and Arrays
Variable Scope – Storage Classes
Storage Class
In C programming, a storage class is a keyword that defines the lifetime, scope, and
memory location of a variable. It determines how a variable is stored and accessed.
1. Automatic (auto)
2. Register (register)
3. Static (static)
4. External (extern)
5. Typedef (typedef)
1. Automatic (auto)
Default storage class. Variables are allocated on the stack and destroyed when the function
returns.
Example:
void func()
{
auto int x = 10; // equivalent to int x = 10;
printf("%d", x);
}
2. Register (register)
Example:
void func()
{
register int x = 10;
for (int i = 0; i < 100; i++) {
x += i;
}
printf("%d", x);
}
3. Static (static)
Example:
void func()
{
static int x = 10;
printf("%d", x++);
}
4
Unit-II Functions and Arrays
Variable Scope – Storage Classes
int main()
{
func(); // prints 10
func(); // prints 11
return 0;
}
4. External (extern)
file1.c:
int global_var = 10;
file2.c:
extern int global_var;
void func()
{
printf("%d", global_var);
}
5. Typedef (typedef)
Example:
typedef int MY_INT;
MY_INT x = 10;
In summary:
- auto (default): stack-based, function scope
- register: register-based, function scope
- static: retained value, function scope
- extern: shared across files, global scope
- typedef: type definition, no storage implications
1. Efficient memory usage: Variables are automatically deallocated when out of scope.
2. Reduced memory leaks: No need to manually free memory.
3. Faster execution: Variables are stored on the stack, providing quick access.
5
Unit-II Functions and Arrays
Variable Scope – Storage Classes
Static (static) Benefits:
General Benefits:
1. Memory management: Storage classes help manage memory efficiently.
2. Code organization: Storage classes help organize code and reduce complexity.
3. Performance optimization: Storage classes can improve performance by reducing
memory access and improving data locality.
4. Code reusability: Storage classes enable code reusability by allowing variables to be
shared across functions and files.
5. Error reduction: Storage classes help reduce errors by ensuring variables are properly
initialized and accessed.
Best Practices:
By understanding the benefits and best practices of storage classes, you can write more
efficient, organized, and effective C code.