0

I create a simple test.c file:

#include <stdio.h>

int a;

int a = 100;

void printA(void)
{
        printf("a is %d\n", a);
}

Compile it to generate object file:

$ gcc -c test.c
$ 

It is OK! But per my understanding, the variable a should be redefined, is it right?

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164

1 Answers1

-1

A variable is local to block it is defined.Once code in block is executed variable goes out of scope.In your case firstly a is defined globally.In second case it is defined inside a function.Thus, to compiler both a is different in both cases not the same.