我编写这段代码是为了知道我的输入是否是二的倍数
\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <unistd.h>\n\nbool main(int ac, char **av)\n{\n if (ac == 2)\n {\n int nb = atoi(av[1]);\n (((nb / 2) * 2) != nb) ? false : true; \n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n但海湾合作委员会正在返回我这个:
\n\ntest.c:5:1: error: unknown type name \xe2\x80\x98bool\xe2\x80\x99\nbool main(int ac, char **av)\n^\ntest.c: In function \xe2\x80\x98main\xe2\x80\x99:\ntest.c:10:32: error: \xe2\x80\x98false\xe2\x80\x99 undeclared (first use in this function)\n (((nb / 2) * 2) != nb) ? false : true;\n ^\ntest.c:10:32: note: each undeclared identifier is reported only once for each function it appears in\ntest.c:10:40: error: \xe2\x80\x98true\xe2\x80\x99 undeclared (first use in this function)\n (((nb / 2) * 2) != nb) ? false : true;\nRun Code Online (Sandbox Code Playgroud)\n\n我在Ubuntu bash for Windows下下(我现在无法访问任何 Linux)
\n\n我不明白为什么我不能在我的函数中使用bool类型,或者为什么' false ' 和 ' true类型,或者为什么无法识别
\n您的代码中有很多问题。
首先你错过了return这里的关键字:
return (((nb / 2) * 2) != nb) ? false : true;
^^^^^^
Run Code Online (Sandbox Code Playgroud)
除此之外,您不需要三元运算符,因为第一部分已准备好布尔值。所以简单地做:
return (((nb / 2) * 2) == nb);
^^^^^^ ^^
Run Code Online (Sandbox Code Playgroud)
此外,当不等于 2return时,代码中没有语句。ac
您还应该包括stdbool.h使用bool.
最后,该main函数必须返回int- 而不是bool。
您的代码重写可能是:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdbool.h> // Include this to use bool
bool foo(int ac, char **av)
{
if (ac == 2)
{
int nb = atoi(av[1]);
return (((nb / 2) * 2) == nb);
}
return false;
}
int main(int ac, char **av)
{
if (foo(ac, av))
{
printf("foo returned true\n");
}
else
{
printf("foo returned false\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4779 次 |
| 最近记录: |