|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include<stdio.h>
- int strlenown(const char* psz)
- {
- int len;
- while(*(psz++))
- len++;
- return len;
- }
- int main()
- {
- int a;
- char m[100]={'i',' ','l','o','v','e',' ','c','h','i','n','a','\0'};
- a=strlenown(m);
- printf("%d",a);
- return 0;
- }
复制代码
为啥定义len没有初始化为零对程序没有影响呢?
本帖最后由 jackz007 于 2020-12-19 13:04 编辑
与编译器有关,用 gcc 编译的符合预期,但是,用 VC 编译的就不行:
gcc
- D:\00.Excise\C>g++ -o x x.c
- D:\00.Excise\C>x
- 12
- D:\00.Excise\C>
复制代码
用 VC 16.8.2 编译:
- D:\00.Excise\C>cl x.c
- 用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.28.29334 版
- 版权所有(C) Microsoft Corporation。保留所有权利。
- x.c
- D:\00.Excise\C\x.c(7) : warning C4700: 使用了未初始化的局部变量“len”
- Microsoft (R) Incremental Linker Version 14.28.29334.0
- Copyright (C) Microsoft Corporation. All rights reserved.
- /out:x.exe
- x.obj
- D:\00.Excise\C>x
- 18026637
- D:\00.Excise\C>
复制代码
|
|