鱼C论坛

 找回密码
 立即注册
查看: 1012|回复: 3

[已解决]有关局部变量的问题

[复制链接]
发表于 2022-1-5 18:52:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>

  2. int main()
  3. {
  4.         int t=1;
  5.         {
  6.                 int t=2;
  7.                 printf("%d\n",t);
  8.                 {
  9.                         t=3;       
  10.                         printf("%d\n",t);       
  11.                 }
  12.                 printf("%d\n",t);       
  13.         }
  14.         printf("%d\n",t);
  15.         return 0;
  16. }
复制代码


输出是2、3、3、1,为什么不是2、3、2、1呢?
最佳答案
2022-1-5 20:09:22
本帖最后由 jackz007 于 2022-1-5 20:24 编辑

        本代码在第 5 、7 行分两次定义了同名局部变量 t,所以,在下面的代码中,红色区域和蓝色区域的 t 不是同一个局部变量。

int main()
{
        int t=1;                                                // 定义新的局部变量
        {
                int t=2;                                        // 定义新的局部变量
                printf("%d\n",t);                       // 打印 2
                {
                        t=3;                                     // 为局部变量赋值,并非定义新局部变量
                        printf("%d\n",t);               // 打印 3      
                }
                printf("%d\n",t);                       // 打印 3      
        }

        printf("%d\n",t);                               // 打印 1
        return 0;
}

        这样再看,打印出 2、3、3、1 是不是很合乎逻辑?
        下面的代码才会打印 2、3、2、1
int main()
{
        int t=1;                                                // 定义新的局部变量
        {
                int t=2;                                        // 定义新的局部变量
                printf("%d\n",t);                       // 打印 2
                {
                        int t=3;                                 // 定义新的局部变量
                        printf("%d\n",t);               // 打印 3      
                }

                printf("%d\n",t);                       // 打印 2      
        }

        printf("%d\n",t);                               // 打印 1
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-1-5 19:29:49 | 显示全部楼层
第 10 行, t = 3
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-5 20:09:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-1-5 20:24 编辑

        本代码在第 5 、7 行分两次定义了同名局部变量 t,所以,在下面的代码中,红色区域和蓝色区域的 t 不是同一个局部变量。

int main()
{
        int t=1;                                                // 定义新的局部变量
        {
                int t=2;                                        // 定义新的局部变量
                printf("%d\n",t);                       // 打印 2
                {
                        t=3;                                     // 为局部变量赋值,并非定义新局部变量
                        printf("%d\n",t);               // 打印 3      
                }
                printf("%d\n",t);                       // 打印 3      
        }

        printf("%d\n",t);                               // 打印 1
        return 0;
}

        这样再看,打印出 2、3、3、1 是不是很合乎逻辑?
        下面的代码才会打印 2、3、2、1
int main()
{
        int t=1;                                                // 定义新的局部变量
        {
                int t=2;                                        // 定义新的局部变量
                printf("%d\n",t);                       // 打印 2
                {
                        int t=3;                                 // 定义新的局部变量
                        printf("%d\n",t);               // 打印 3      
                }

                printf("%d\n",t);                       // 打印 2      
        }

        printf("%d\n",t);                               // 打印 1
        return 0;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-5 22:31:51 | 显示全部楼层
作用域不同
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 02:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表