这是一个新手问题。我正在编写这个简单的 if...else 代码,但 gcc 编译器认为 ';' else 表达式之后缺少 token,尽管我从未见过任何在函数大括号之前包含此特定 token 的 C if...else 代码示例。另外,当我在“else”函数后面包含标记时,终端会输出 if 和 else 元素,所以我在这里完全迷失了。
编译器错误:
cclasses.c:15:2: error: expected ‘;’ before ‘{’ token
代码:
#include <stdio.h>
#include <stdbool.h>
int main ()
{
int x;
printf("\nEnter with negative or positive int.");
scanf("%i", &x);
if (x > 0)
{
printf("\nYou've entered positive int: %i\n", x);
}
else(x < 0)
{
printf("\nYou've entered negative int: %i\n", x);
}
printf("\nEND OF PROGRAM.\n");
return 0;
}
最佳答案
我这样做了,效果很好。
int main ()
{
int x;
printf("\nEnter with negative or positive int.");
scanf("%i", &x);
if (x > 0)
{
printf("\nYou've entered positive int: %i\n", x);
}
else if (x == 0)
{
printf("\nYou've entered zero/null: %i\n", x);
}
else
{
printf("\nYou've entered negative int: %i\n",x);
}
printf("\nEND OF PROGRAM.\n");
return 0;
}
关于C 中的编译器错误 - ELSE 语句后的 ‘;’ 标记之前应有 ‘{’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53573819/