declaration of variables with same name as global,local and static

前端 未结 5 1542
北海茫月
北海茫月 2020-12-30 17:07

I have the following code snippet and I have to analyse what the output will be:

#include 

  void f(int d);

  int a = 1, b = 2, c = 3, d = 4         


        
5条回答
  •  猫巷女王i
    2020-12-30 17:42

    This output actually makes sense.

    In C/C++, the identifiers in a given scope are given preference over the identifiers in the outer scope. In this case in the function main, variables a and c will be used as local variables and rest b and d as global variables. Similarly, in the function void f(int d), d is the passed parameter, a will be used as static whenever the function is called, b and c will be used as global variables. Hence the output will be calculated.

    However you have shown the incorrect output. Correct output must be :

    7 12 4 4 14 26 5 11 21 27 6 5 5 27 6 4

提交回复
热议问题