Site icon TestingDocs.com

C Local Variables

Overview

Variables that are declared inside a block or function are known as local variables. The scope and visibility of the local variable are limited to the block or the function in which the variable is defined.

The scope of the variable is a region of the C program where the variable can be referenced or used in the program.

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 that are inside that block of code or function. The lifetime of the variable 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