C 中的编译器错误 - ELSE 语句后的 ‘;’ 标记之前应有 ‘{’

标签 c if-statement gcc token

这是一个新手问题。我正在编写这个简单的 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/

相关文章:

c++ - 两次 GCC 编译相同的输入,生成两个不同的代码(第二个错误)

c++ - "optimized out"值是否应该是随机的?

使用 gcc 编译 PowerPC 二进制文件并限制可用寄存器

c - printf 转换规范的大小限制

ios - 如何在 Switch 语句中使用可选整数

c - 测量 L1 和 L2 缓存的大小和顺序

c - 如何使用 if else 进行字符串比较?

c - 我不知道我的角色需要什么条件

c - 在循环中对带空格的字符串使用 scanf 时的有趣行为

c - Linux C,为什么 fcntl 作用于 STDIN 也会影响 STDOUT 和 STDERR?