Apply for Zend Framework Certification Training

c



< Variables in C Constants in C >



Scope Rules in C
In C programming, scope refers to the portion or region of a program where a variable or identifier can be accessed directly. If we try to access a variable outside its scope, the compiler will report an error because the identifier is not available in that region.
In simple words:
Scope determines where a variable can be declared, accessed, and used in a C program.
C follows lexical (static) scoping, which means the scope of a variable is determined by its position in the source code at compile time. It does not depend on which function calls another function.
Example of Variable Scope
#include <stdio.h>
int main(){
    // var is available only inside main()
    int var = 34;
    printf("%d", var);
    return 0;
}
// var cannot be accessed here
void func(){
    printf("%d", var);
}
Output
error: 'var' undeclared
Explanation
The variable var is declared inside the main() function. Therefore, its scope is limited to main().
The function func() is outside the scope of var, so it cannot access that variable.
This demonstrates an important rule:
A variable can only be accessed within the region where its scope is valid.
Types of Scope in C
The major scopes commonly discussed in C are:
Global/File Scope
Local/Block Scope
Function Scope – mainly applicable to labels
Function Prototype Scope – applicable to parameter names in function declarations
1. Global Scope in C
The global scope is the area of a C program outside all functions and blocks.
A variable declared in this area is called a global variable.
Global variables can normally be accessed by functions defined after their declaration in the same source file.
Global scope is also commonly called file scope because the identifier's scope extends from its declaration to the end of the source file.
Example
#include <stdio.h>
// Global variable
int global = 5;
void display(){
    printf("%d\n", global);
}
int main(){
    printf("Before change within main: ");
    display();
    // Changing the global variable
    global = 10;
    printf("After change within main: ");
    display();
    return 0;
}
Output
Before change within main: 5
After change within main: 10
Explanation
The variable global is declared outside all functions, so it has global/file scope.
Both main() and display() can access it.
When main() changes:
global = 10;
the new value is also visible to display().
Linkage of Global Variables
Scope and linkage are related concepts, but they are not the same thing.
A global variable normally has external linkage, which means it can be referred to from another source file using the extern keyword.
For example, suppose we have two files.
file1.c
#include <stdio.h>
int a = 0;
void myfun(){
    printf("%d\n", a);
}
Here, a is a global variable.
main.c
#include <stdio.h>
extern int a;
void myfun();
int main(){
    a = 2;
    myfun();
    return 0;
}
Output
2
Explanation
The statement:
extern int a;
tells the compiler that a is defined somewhere else, in this case in file1.c.
Both source files can therefore work with the same global variable.
The two files would typically be compiled and linked together, for example:
gcc main.c file1.c -o program
Static Global Variables
If we want a global variable to be accessible only within the current source file, we can use the static keyword.
#include <stdio.h>
static int count = 10;
int main(){
    printf("%d", count);
    return 0;
}
Here, count has file scope but internal linkage, so it cannot be accessed directly from another source file.
2. Local Scope in C
A variable declared inside a function or block has local/block scope.
A block is a section of code enclosed within curly braces:
{
    // block
}
Local variables can be accessed within their own block and, where applicable, by nested blocks.
Example
#include <stdio.h>
int main(){
    int x = 10;
    int y = 20;
    {
        printf("x = %d, y = %d\n", x, y);
        {
            int y = 40;
            x++;
            y++;
            printf("x = %d, y = %d\n", x, y);
        }
        printf("x = %d, y = %d\n", x, y);
    }
    return 0;
}
Output
x = 10, y = 20
x = 11, y = 41
x = 11, y = 20
Explanation
Initially:
int x = 10;
int y = 20;
Both variables belong to the outer block.
Inside the nested block, another variable named y is declared:
int y = 40;
This creates a new variable that temporarily hides the outer y.
Therefore:
x = 11
y = 41
inside the inner block.
After leaving the inner block, the inner y no longer exists, so the program again accesses the outer y:
x = 11, y = 20
Variable Shadowing
When a variable declared in an inner block has the same name as a variable in an outer block, the inner variable shadows the outer variable.
Example:
#include <stdio.h>
int main(){
    int value = 10;
    {
        int value = 20;
        printf("%d\n", value);
    }
    printf("%d\n", value);
    return 0;
}
Output
20
10
Inside the inner block, value refers to the inner variable.
After the inner block ends, the outer value becomes accessible again.
Important Points About Scope
Scope Where declared Where accessible
Global/File Scope Outside all functions From its declaration to the end of the file
Block/Local Scope Inside { } Within that block and appropriate nested blocks
Function Scope Labels inside a function Throughout that function
Function Prototype ScopeParameter names in prototypes Only within the function prototype
Key Rules
Scope determines where an identifier can be accessed.
A local variable cannot normally be accessed outside its block.
A global variable can be accessed by functions in the same source file, subject to its linkage and declaration.
The extern keyword can be used to refer to an externally defined global variable.
The static keyword can restrict a file-scope variable to the current source file.
An inner variable can shadow a variable with the same name in an outer scope.
C uses lexical/static scoping, so scope is determined from the source-code structure.
Simple Way to Remember
Global Variable
       ↓
Available throughout its file
       ↓
--------------------------------
       ↓
Function / Block
       ↓
Local Variable
       ↓
Available only within its scope
In short: Scope tells us “Where can I use this variable?”, while linkage tells us “Can this identifier refer to the same entity from another scope or source file?”

< Variables in C Constants in C >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top