C 变量的作用域
下面的程序輸出什么?思考一下
#include <stdio.h>// Driver Code
int main()
{{int x = 10, y = 20;{// The outer block contains// declaration of x and// y, so following statement// is valid and prints// 10 and 20printf("x = %d, y = %d\n", x, y);{// y is declared again,// so outer block y is// not accessible in this blockint y = 40;// Changes the outer block// variable x to 11x++;// Changes this block's// variable y to 41y++;printf("x = %d, y = %d\n", x, y);}// This statement accesses// only outer block's// variablesprintf("x = %d, y = %d\n", x, y);}}return 0;
}
Output:?
x = 10, y = 20
x = 11, y = 41
x = 11, y = 20
實例代碼來自:https://www.geeksforgeeks.org/scope-rules-in-c/?
總結
- 上一篇: 求一个早晨个性签名。
- 下一篇: C运算符优先级笔记