在 C 编程中,我真的对 atoi 函数感到困惑。
我也可以这样写:
atoi(年) = 年龄;
而不是:
年龄 = atoi(年);
。
第一个给出了警告,但仍然像第二个一样工作。哪一个是正确的?
最佳答案
不,您不能使用 atoi
或任何其他函数来做到这一点。如果 C 编译器允许这样做并生成从 atoi(years)
分配到 age
的代码,则编译器将损坏。
为了好玩,我把它放入一个小程序中:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *years = "61";
int age;
atoi(years) = age;
printf("%d\n", age);
}
并通过我的本地 C 编译器(HP C/aC++ 编译器 FWIW)卡住了它。产生了以下消息:
$ cc test.c
"test.c", line 9: error #2137: expression must be a modifiable lvalue
atoi(years) = age;
^
"test.c", line 9: warning #2549-D: variable "age" is used before its value is
set
atoi(years) = age;
^
1 error detected in the compilation of "test.c".
祝你好运。
关于C语言编程,atoi函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51328806/