C Programming language-C Variable Scopes

C Variable Scopes

The scope of the variables refers to the block of the program in which the variables can be accessed after it is declared. In C, each and every variable defined in scope. The scope is the section or region of a program only where there is an existence for the variable and the variable cannot be accessed outside the scopes.

The variables can be declared in three places, such as

Local Variables: It is declared inside the function or a block.
Global Variables: It is declared out of the functions.
Formal Parameters: The variable is declared in the function parameters.

Local Variables

The variable that is declared inside the block and can be used inside the same block is called local variables.

Local Scope or Block Scope

A local scope or block scope is a collective program statement that is declared inside the block and variables which are declared inside this block is called local variables. This block is enclosed with the left ({) and the right (}) curly braces. There can be a block inside another block in which the variables in the scope can be accessed within the scope only. For example, if the variable is declared inside the inner block of another block, then the variable declared inside the inner block cannot be accessed outside of the block.

The following is the example code for local scope:

Example:
#include <stdio.h>
int main()
{
    /* Locak variable definition & initialization */
    int a, b, c;

    /* Actual variable initialization */
    a = 5;
    b = 10;
    c = a+b;

    printf("The local variables are\na = %d \nb = %d \nc = %d", a,b,c);

    return 0;
}
Output:
The local variables are
a = 5
b = 10
c = 15
Global Variables

The variables that are declared outside the function block and can be accessed inside the function are called Global variables.

Global Scope

Generally, the global variables are defined outside the function or specific block. In most of the cases, it is defined on top of the C Program. These variables hold their respective variables throughout the end of the program. 
Any function in the program can access the global variables i.e., the availability of the variable stays for the entire program after being declared.

The following code shows the global variable 

Example:
#include <stdio.h>
int z = 10; /* Global variable is defined and initialized */
int main()
{
    printf("Global variable z = %d", z);

    return 0;
}
Output:
Global variable z = 10

Global Variable Initialization

After we define local variables, the system or a compiler will not be initializing any value for it. The local variable should be initialized by the programmer. It is always considered as the best practice to initialize the variables before using it. Whereas the global variables are initialized automatically by the compiler as and when we define. The following is the data type and how the global variables are initialized in default:


Datatype
Initial Default value
int
0
char
‘/0’
float
0
double
0
pointer
NULL