Redeclaration of global variable vs local variable

后端 未结 3 1277
太阳男子
太阳男子 2020-11-30 07:17

When I compile the code below

#include

int main()
{
  int a;
  int a = 10;
  printf(\"a is %d \\n\",a);
  return 0;
}

I get

3条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 07:48

    The other reason I could think of is that un-initialized global variables are stored in the BSS (Block Structured Segment) where are the global variables that are initialized are stored in Data Segment.

    I am guessing that there is some kind of a name space resolution and when there is a conflict the variable in the Data segment overrides the one in the Block Structured Segment.

    if you were to declare

    int a =5 int a = 10

    in the global scope (both in the data segment) there would be conflict as expected.

提交回复
热议问题