花括号内的变量声明

Deb*_*day -2 c curly-braces variable-declaration printf-debugging

为什么以下代码会产生错误?我不明白为什么花括号会有所作为.

#include<stdio.h>

int main(void)
{
    {
        int a=3;
    }

    {
        printf("%d", a); 
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Jab*_*cky 6

局部变量的范围仅限于{}之间的块.

换句话说:包含的块外部int a=3; a不可见.

#include<stdio.h>
int main()
{
    {
      int a=3;
      // a is visible here
      printf("1: %d", a);  
    }

    // here a is not visible
    printf("2: %d", a);  

    {
     // here a is not visible either
      printf("3: %d", a); 
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

提示:google c范围变量