C Variable Scope

1. What is Variable Scope?

  • Scope defines the region of a program where a variable can be accessed or modified.

  • Variables in C can be local, global, or static, each with different visibility.


2. Types of Variable Scope

A) Local Variables

  • Declared inside a function or block.

  • Accessible only within that function or block.

  • Destroyed when the function/block ends.

Example:

#include <stdio.h>

void func() {
int localVar = 10; // local variable
printf("Inside func: %d\n", localVar);
}

int main() {
func();
// printf("%d", localVar); // Error: localVar not accessible here
return 0;
}

Output:

Inside func: 10

B) Global Variables

  • Declared outside all functions (usually at the top).

  • Accessible by all functions in the same file.

Example:

#include <stdio.h>

int globalVar = 50; // global variable

void func() {
printf("Inside func: %d\n", globalVar);
}

int main() {
printf("Inside main: %d\n", globalVar);
func();
return 0;
}

Output:

Inside main: 50
Inside func: 50

C) Static Variables

  • Declared using static keyword inside a function.

  • Retains value between function calls.

  • Scope is local to the function, but lifetime is throughout the program.

Example:

#include <stdio.h>

void counter() {
static int count = 0; // static variable
count++;
printf("Count = %d\n", count);
}

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

Output:

Count = 1
Count = 2
Count = 3

Without static, count would reset to 0 on each call.


D) Block Scope

  • Variables declared inside {} are only valid within that block.

#include <stdio.h>

int main() {
int x = 5;
{
int y = 10; // block scope
printf("x = %d, y = %d\n", x, y);
}
// printf("%d", y); // Error: y not accessible here
return 0;
}


E) Function Parameters

  • Function parameters are local to that function.

#include <stdio.h>

void greet(char name[]) {
printf("Hello, %s\n", name); // name is local to greet
}

int main() {
greet("Alice");
// printf("%s", name); // Error: name not accessible here
return 0;
}


3. Summary Table of Variable Scope

Variable Type Scope Lifetime Example
Local Inside function/block Function/block ends int x inside function
Global Entire file/functions Entire program int g = 10; at top
Static (local) Inside function Entire program static int count = 0;
Function parameter Inside function Function call ends void f(int n)

4. Key Points

  1. Local variables are created and destroyed each function call.

  2. Global variables are accessible anywhere in the file.

  3. Static variables retain their value across multiple calls.

  4. Use minimal global variables to avoid unexpected modifications.

  5. Function parameters are also local variables with values passed from the caller.

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

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