【问题标题】:Compiler wants ";" - I don't know why (in C) [closed]编译器想要“;” - 我不知道为什么(在 C 中)[关闭]
【发布时间】:2014-08-09 21:35:53
【问题描述】:

在处理项目欧拉问题(#10)时,我遇到了一个有趣的编译错误。我的编译器告诉我我需要一个分号。确切的错误是49: ';' expected

现在我知道这在 C 代码中并不罕见,但问题是我已经三次检查了所有分号、括号和方括号,但没有遗漏。甚至没有循环。我很可能只是想念它。

但是 编译器告诉我49: ';' expected - 但第 49 行的代码只是一个括号 ({)。

有没有人知道这里可能出了什么问题?

代码如下:

// this is a program to find the sum of all primes below 2 million
// uses the Seive of Eratosthenes

#include <stdio.h>

int main() {
    long int sum = 0;
    long int s = 2000000; // size of array
    long int i;           // 'for' loops
    long int n;           // number to test
    long int a;           // prime checker
    long int multiples;

    long int* array; // define pointer for calloc int array
    // calloc the array
    array = (int*)calloc(s, sizeof(int)); // allocates 2,000,000 spots (s) of
                                          // size "int" to array "array"
    // set all array values equal to 1
    for (i = 0; i < 2000000; i++) {
        array[i] = 1;
    }

    /*
    VALUES IN ARRAY
    The values of the array indicates whether number-
    (0) IS PRIME
    (1) UNTESTED
    (2) IS COMPOSITE
    */

    // implement prime finder
    for (n = 0; n < 2000000; n++) {
        if (array[n] == 0) // IF n IS PRIME
        {
            multiples = n;
            multiples += n;

            while (multiples < 2000000) {
                array[multiples] = 2;
                multiples += n;
            }
        } else
            (array[n] == 2) // IF n IS COMPOSITE (is a multiple)
            { // THIS IS LINE 49
                continue;
            }
        else(array[n] == 1) // UNTESTED
        {
            for (a = 2; a < n; a++) // double checks for composites
            {
                // tests factors
                if (n % a == 0) {
                    printf("ERROR");
                    goto end;
                }
            }

            array[n] = 0;
            multiples = n;
            multiples += n;

            while (multiples < 2000000) {
                array[multiples] = 2;
                multiples += n;
            }
        }
    }

    // read array and sum primes
    for (n = 0; n < 2000000; n++) {
        if (array[n] == 0) // IF n IS PRIME
        {
            sum += n;
        } else
            (array[n] == 2) // IF n IS COMPOSITE
            {
                continue;
            }
        else(array[n] == 1) // IF n MAY/MAY NOT BE PRIME
        {
            printf("ERROR");
            goto end;
        }
    }

    printf("The sum of all primes < 2,000,000 is... %ld!\n", sum);

end:
    getchar();
    return 0;
}

【问题讨论】:

  • 几乎没有发布的代码与事件相关。确保将测试用例简化为最小形式(在这种情况下,许多问题变得不言而喻)。
  • 兄弟,你有else if吗?

标签: c compiler-errors


【解决方案1】:

else改成else if就好了。

然后使用 all else 来做到这一点。

【讨论】:

    【解决方案2】:
    else( array[n] == 2) // IF n IS COMPOSITE (is a multiple)
        {                 //THIS IS LINE 49
    

    else 不带条件。 else 应该是 else if

    编译器的语法恢复会尝试建议最简单的更改,以生成语法上有效的程序。通常,建议的更改没有多大意义。在这种情况下,在else 之后添加; 会将下面的( array[n] == 2) 变成表达式语句 的开头(后面必须跟另一个分号)。

    如果您的代码产生语法错误消息,最好的方法通常是忽略它提出的建议,只查看它引用的行和它前面的行,然后图找出当时的语法有什么问题。然后修复错误并重新编译。

    【讨论】:

      【解决方案3】:

      你真的缺少一些 ifs 来搭配你的 elses:

      if( array[n] == 0) // IF n IS PRIME
      {
          sum += n;
      }
      else if( array[n] == 2) // IF n IS COMPOSITE
      {
          continue;
      }
      else if( array[n] == 1) // IF n MAY/MAY NOT BE PRIME
      {
          printf("ERROR");
          goto end;
      }
      

      这样的编译器很容易混淆,当你这样做时,它的一些错误消息可能会变得难以理解。

      【讨论】:

        猜你喜欢
        • 2023-03-29
        • 2011-05-19
        • 2012-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多