鱼C论坛

 找回密码
 立即注册
查看: 682|回复: 1

[已解决]初始化问题

[复制链接]
发表于 2020-12-19 11:20:35 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<stdio.h>

  2. int strlenown(const char* psz)
  3. {
  4.         int len;
  5.         while(*(psz++))
  6.         len++;
  7.         return len;
  8. }


  9. int main()
  10. {
  11.         int a;
  12.         char m[100]={'i',' ','l','o','v','e',' ','c','h','i','n','a','\0'};
  13.         a=strlenown(m);
  14.         printf("%d",a);
  15.         return 0;
  16. }
复制代码

为啥定义len没有初始化为零对程序没有影响呢?
最佳答案
2020-12-19 13:03:51
本帖最后由 jackz007 于 2020-12-19 13:04 编辑

      与编译器有关,用 gcc 编译的符合预期,但是,用 VC 编译的就不行:
gcc
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 12
  4. D:\00.Excise\C>
复制代码

    用 VC 16.8.2 编译:
  1. D:\00.Excise\C>cl x.c
  2. 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
  3. 版权所有(C) Microsoft Corporation。保留所有权利。

  4. x.c
  5. D:\00.Excise\C\x.c(7) : warning C4700: 使用了未初始化的局部变量“len”
  6. Microsoft (R) Incremental Linker Version 14.28.29334.0
  7. Copyright (C) Microsoft Corporation.  All rights reserved.

  8. /out:x.exe
  9. x.obj

  10. D:\00.Excise\C>x
  11. 18026637
  12. D:\00.Excise\C>
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-12-19 13:03:51 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2020-12-19 13:04 编辑

      与编译器有关,用 gcc 编译的符合预期,但是,用 VC 编译的就不行:
gcc
  1. D:\00.Excise\C>g++ -o x x.c

  2. D:\00.Excise\C>x
  3. 12
  4. D:\00.Excise\C>
复制代码

    用 VC 16.8.2 编译:
  1. D:\00.Excise\C>cl x.c
  2. 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
  3. 版权所有(C) Microsoft Corporation。保留所有权利。

  4. x.c
  5. D:\00.Excise\C\x.c(7) : warning C4700: 使用了未初始化的局部变量“len”
  6. Microsoft (R) Incremental Linker Version 14.28.29334.0
  7. Copyright (C) Microsoft Corporation.  All rights reserved.

  8. /out:x.exe
  9. x.obj

  10. D:\00.Excise\C>x
  11. 18026637
  12. D:\00.Excise\C>
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-7 13:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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