Site icon TestingDocs.com

C Local Variables

Overview

Variables declared inside a block or function are known as local variables. Their scope and visibility are limited to the block or function in which they are defined. The scope of the variable is a region of the C program where the variable can be referenced or used.

Local Variable

The local variable is stored on the Stack. The lifetime is limited to the block or the function
in which the variable is defined.

They can be used only by the programming statements inside the block of code or function. The variable’s lifetime ends when the block or function completes execution.

Example

Following is the example that explains the concepts that we have learned:

/**
*************************************************
 * Program Description:
 * C Local Variable Demo
 * Filename: local.c
 * C Tutorials - www.TestingDocs.com
 ************************************************
 */
#include

int main()
{
    /* local variable  of the main function */
    int outer = 10;
    {
        /* local variable  of the block  */
        int inner = 20;
        printf(" The inner variable is alive inside the block = %d \n",inner);
        printf(" The outer variable is alive & visible in the block = %d \n",outer);
    }
    printf(" The inner variable is dead outside the block. \n");
    printf(" The outer variable is alive & visible in the main() = %d \n",outer);

    return 0;
}

 

C Tutorials

C Programming Tutorials on this website:

https://www.testingdocs.com/c-language-tutorial/

Exit mobile version