编译器想要 ";"- 我不知道为什么(在 C 中)

标签 c compiler-errors

在处理项目欧拉问题(#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 更改为 else if 就可以了。

并用所有其他来做到这一点。

关于编译器想要 ";"- 我不知道为什么(在 C 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25223290/

相关文章:

c++ - 奇异矩阵的高斯消元

有人可以解释一下以下代码中如何将内存分配给双指针吗

c++ - C1083 WIN32 没有那个文件或目录

c - '=' token 之前出现错误 : expected ',' , ';' 、 'asm' 、 '__attribute__' 或 '{'

C-警告 : control reaches end of non-void function

c - 在 C linux 中打开文件

c++ - 在我的网站上运行python,C,C++应用程序

c - 在预先存在的 R 包中模仿 C 的用法

C程序错误: expected expression before int

compiler-errors - 推力(CUDA库)编译错误,例如 “' vectorize_from_shared_kernel__entry': is not a member of 'thrust::detail::device::cuda' ”