VOOZH about

URL: https://www.geeksforgeeks.org/c/scope-rules-in-c/

⇱ Scope rules in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Scope rules in C

Last Updated : 17 Oct, 2025

The scope of a variable in C is the block or the region in the program where a variable is declared, defined, and used. Outside this region, we cannot access the variable, and it is treated as an undeclared identifier.

  • The scope of an identifier is the part of the program where the identifier may directly be accessible.
  • All variables are lexically(or statically) scoped in C which means the scope is defined at the compiler time and not dependent on the caller of the function.


Output

solution.c: In function 'func':
solution.c:15:28: error: 'var' undeclared (first use in this function)
 void func() { printf("%d", var); }

Here, we tried to access variable names var As we can see that if we try to refer to the variable outside its scope, we get the above error.

Global Scope in C

The global scope refers to the region outside any block or function.

  • The variables declared in the global scope are called global variables.
  • Global variables are visible in every part of the program.
  • Global is also called File Scope as the scope of an identifier starts at the beginning of the file and ends at the end of the file.

Output
Before change within main: 5
After change within main: 10

Linkage of Variables in Global Scope

Global variables have external linkage by default. It means that the variables declared in the global scope can be accessed in another C source file. We have to use theexternkeyword for that purpose.

Example of External Linkage

file1.c


main.c


Output

2

Note: To restrict access to the current file only, global variables can be marked as static.

Local Scope in C

The local scope refers to the region inside a block or a function. It is the space enclosed between the { } braces.

  • The variables declared within the local scope are calledlocal variables.
  • Local variables are visible in the block they are declared in and other blocks nested inside that block.
  • Local variables have no linkage.

Output
x = 10, y = 20
x = 11, y = 41
x = 11, y = 20
Comment