2 answers
Best answer
Scope of the variable can be defined as the part of the code area where the variables declared in the program can be accessed directly. In C, all identifiers are lexically (or statically) scoped.
Coder B.
· 15 May 2022
Report
Was this answer helpful?
Thanks for the feedback.
Sign up free to save answers and ask your own doubts
The scope of a variable is the region of the program in which the variable is visible and can be used. In C, variables mainly have block (local) scope or file (global) scope.
- Block (local) scope: A variable declared inside a block
{ }, for example inside a function or a loop, can be used only from its declaration to the end of that block. Function parameters also have block scope (the function body). - File (global) scope: A variable declared outside all functions can be used from its declaration to the end of the source file, by every function that follows. With
externit can also be used in other files; declaring itstatickeeps it private to that file. - Function scope: applies only to labels used with
goto; a label is visible anywhere in its function. - Function prototype scope: parameter names in a prototype, e.g.
int add(int a, int b);, are visible only up to the end of that prototype.
int count = 0; /* file (global) scope */
void show() {
int x = 5; /* block (local) scope: only inside show() */
count++; /* global variable is visible here */
}C uses lexical (static) scoping: whether a name is visible is decided by where it is written in the program text. If a local variable has the same name as a global one, the local variable hides the global one inside its block.
Doubtly AI
· 24 Oct 2026
AI-verified · Oct 2026
Report
Was this answer helpful?
Thanks for the feedback.
Sign up free to save answers and ask your own doubts
Your answer
Log in or create a free account to answer this question.