What will be its output and why?

后端 未结 3 1944
庸人自扰
庸人自扰 2021-01-29 16:44

I was going through scope rules questions and all and then got a code snippet, below:

#include 
int main()
{
  int x = 1, y = 2, z = 3;
  printf(\         


        
3条回答
  •  爱一瞬间的悲伤
    2021-01-29 17:11

    z = 1077149696
    Using %d to print float values is undefined behaviour.
    Use "%f" instead

    1. All the variables you have used have storage type "Auto" or "Automatic".
    2. The scope of the automatic variable lies inside the block in which it is declared.
    3. If there are nested blocks, then the variable declared in the outermost block will be visible to all other blocks.
    4. In case if a block has a declared a variable that matches with the one declared in outer blocks, then it will overwrite the outer variable "in its block" i.e.(locally).

    To sum up : Automatic variables are local to the block in which they are declared.

提交回复
热议问题