Variable defined in an inner block with the same name as one in the outer block will shadow the one in outer block - C Language Basics

C examples for Language Basics:Variable

Description

Variable defined in an inner block with the same name as one in the outer block will shadow the one in outer block

Demo Code

#include <stdio.h>

int main(){/*w  w  w.  ja v a2s .c  om*/
    int x = 30;      // original x
    
    printf("x in outer block: %d at %p\n", x, &x);
    {
        int x = 77;  // new x, hides first x
        printf("x in inner block: %d at %p\n", x, &x);
    }
    printf("x in outer block: %d at %p\n", x, &x);
    
    while (x++ < 33) // original x
    {
        int x = 100; // new x, hides first x 
        x++;
        printf("x in while loop: %d at %p\n", x, &x);
    }
    printf("x in outer block: %d at %p\n", x, &x);

    return 0;
}

Result


Related Tutorials