Search code examples
cglobal-variableslocal-variables

Values of local and global variable values in C


I don't seem to understand what's happening here

#include <stdio.h>

int var = 5;

int main(){
    int var = var;
    printf("%d",var);
    return 0;
}

Why does this program print a garbage value when the value of var is assigned to be 5?


Solution

  • the local var has priority on the global var. So the compiler translate it as (local) var = (local) var