What Is Variable Scope In C Programming?

By Abhishek Roy Last updated : November 25, 2023

c variable scope tutorial

Variables are the major backbone of any programming language & the C programming language is no different from that. For solving any problem with the C programming language, a need to declare one or more variables is essential. But, along with the variable declaration, we have to think about its scope as well.

Are you not aware of the Variable Scope in the C programming language?

Variable Scope or Scope of Variable in the C programming is the specific field in which a variable can be declared, used & modified. In simple terms, the variable is accessible in a certain range in the C programming language. This range is known as the Variable Scope. C Programming language is considered to be the foundation of programming journey for any students who are studying computer science. Students often gets stuck in such concepts and they look for C Assignment Help online. But thanks to developers community and many services available online where students can learn, explore and gain practical knowledge of this language and its related concepts.

If you want to access a variable outside of its scope, it will flag an error as the output & the code will not be executed on the device. In the C programming language, the scope of the variable is get determined by the curly braces ({}) which helps to demark the range of any function or code.

Let us check the following syntax or sample code that will help to understand the Variable Scope in the C programming language.

#include <stdio.h> // Declaration of Header File

int main() // Main Function Starting

{ // Starting Of Variable Scope

  // Other Necessary Statements
  return 0;

} // Ending Of Variable Scope

Types of Variable Scope in C program:

Each of the variables has a separate scope in the C program. If any variable is declared in any function, that variable can only be accessed from that function. However, there is a method where a variable can be utilized by every function; which means there will be no such scope present.

Depending on the range of variables, there are two possible types of variable scopes present in the C program. They are the following:

  1. Local Variable Scope: The Local Variable Scope is a more secure & private one as it is not accessible by other functions. It is bounded with the curly braces ({}) and it can be only accessed from that curly brace.
  2. Global Variable Scope: The Global Variable Scope ensures that a variable can be accessed by any part of a certain code. This means, that each & every function will get access to the variable & it can't be bound with the curly braces ({}).

Let us check the variable scope declaration process with some sample examples.

Local Variable Scope Declaration in C Program:

The Local Variable Scope declaration in the C program can be done with two separate functions. We will declare one variable in the main function & will try to print the variable in the separate function. Following the Local Variable Scope rule, the program will mark an error.

#include <stdio.h> // Declaration Of Header File

int main() // Main Function Declaration

{ // Starting Of Local Variable v in Main Function

  int v = 2023; // Declaring Variable In Main Function
  fu(); // Calling The User Defined Function
  printf("The Result In Main Function: %d", v); // Printing In Main Function
  return 0;

} // Ending of Local Variable v in Main Function

void fu() // A User-Defined Function Declaration
{ // Starting Of Local Variables Declared in Function fu()

  printf("%d", v); // Trying To Print The Value of Variable Declared in Main

} // Ending Of Local Variables Declared in Function fu()

What we have done on the above program?

From the above program, we can see that a variable is declared inside the main function. We have also defined a user-defined function fu() in the program. Before printing the variable in the main function, we will call the fu() function to print the value in that function.

But the variable had the local scope & it can't be accessed from the fu() function. So, the output will have one error that will imply that the variable is not present in the fu() function.

Output:

c variable scope output 1

Global Variable Scope Declaration In C Program:

If you want to access any variable from any point in the C program, then you have to declare that variable as the Global Variable. The Global Variables can be accessed from any kind of function & block. It can be accessed from the Main function as well as from other functions.

#include <stdio.h> // Header File Declaration

int v = 2023; // Declaring In Global Scope
int main() // Main Function Declaration

{ // Starting Of Any Local Variable in Main Function

  printf("The Result In Main Function: %d\n", v); // Printing Global Variable In Main Function
  fu(); // Calling The User Defined Function fu()
  return 0;

} // Ending Of Any Local Variable in Main Function

void fu() // A User-Defined Function
{ // Starting Of Any Local Variable in fu() Function

  printf("The Result In other Function: %d", v); // Trying To Print The Value of Global Variable

} // Ending Of Any Local Variable in fu() Function

What we have done on the above program?

In the above program, you can see that the variable is declared in an odd position. After declaring the header file, the variable is declared along with some values. This particular position is known as the Global position of the program.

Whatever you declare in this position can be accessed from any point in the program. Now, in the main function, we will directly try to print the variable value. After that, we will call another function fu() in the program.

We have not defined the variable in the fu() function as well, but we will try to print it. And as the variable is declared in the Global Position, it will not flag any error. The two print statements will be printed in the output.

Output:

c variable scope output 2

So, as you can see the variable scope is very important to mark which variable should be a private one to any function of the code block which is divided by curly braces ({}). If you want to access any variable in multiple functions, no need to share it as an argument. Rather, make it a Global Variable & access it from any function in the program.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.