Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    C Variable Scope

    A scope is a region of the program, and the scope of variables refers to the area of the program where the variables can be accessed after its declaration. This tutorial guides you on how to use C variable scope.

    In C programming, every variable is defined in scope. You can define scope as the section or region of a program where a variable has its existence; moreover, that variable cannot be used or accessed beyond that region. In C programming, a variable declared within a function differs from a variable declared outside of a function. The variable can be declared in three places. These are:

    Position Type
    Inside a function or a block. local variables
    Out of all functions. Global variables
    In the function parameters. Formal parameters

    So, now let's have a look at each of them individually.

    Local Variables

    Variables that are declared within the function block and can be used only within the function are called local variables.

    Local Scope or Block Scope

    A local scope or block is a collective program statement placed and declared within a function or block (a specific area surrounded by curly braces). Variables lying inside such blocks are termed local variables. All these locally scoped statements are written and enclosed within the left ({) and right (}) curly braces. C also has a provision for nested blocks, which means that a block or function can occur within another block or function. So it means that variables declared within a block can be accessed within that specific block and all other internal blocks of that block but cannot be accessed outside the block.

    Example:

    #include <stdio.h>
     
    int main ()
    {
        //local variable definition and initialization
        int x,y,z;
         
        //actual initialization
        x = 20;
        y = 30;
        z = x + y;
         
        printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
         
        return 0;
    }
    

    Global Variables

    Variables declared outside the function block and accessed inside the function are called global variables.

    Global Scope

    In most cases, global variables are defined outside a function or any specific block on top of the C program. These variables hold their values all through the end of the program and are accessible within any of the functions defined in your program. Any function can access variables defined within the global scope, i.e., its availability stays for the entire program after being declared.

    Example:

    #include <stdio.h>
    
    //global variable definition
    int z;
    
    int main ()
    {
        //local variable definition and initialization
        int x,y;
         
        //actual initialization
        x = 20;
        y = 30;
        z = x + y;
         
        printf ("value of x = %d, y = %d and z = %d\n", x, y, z);
         
        return 0;
    }
    

    Global Variable Initialization

    After defining a local variable, the system or the compiler won't initialize any value to it. You have to initialize it by yourself. It is considered good programming practice to initialize variables before using them. Conversely, the compiler initializes global variables automatically as and when defined. Here's how based on datatype, global variables are defined:

    datatype Initial Default Value
    int 0
    char '\0'
    float 0
    double 0
    pointer NULL


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.