0

I'm solving a basic C problem on a site called UVa Online Judge, and I encounter an error which seems to related to compiler options.

ANSI C 5.3.0 - GNU C Compiler with options: -lm -lcrypt -O2 -pipe -ansi -DONLINE_JUDGE

The problem(with problem id as prefix) is UVa 100 3n+1 problem as following. It can be compiled and run, without error, on Visual Studio 2019, the site reported "Compile Error" for it. The site will not show any error message, just Compile Error in its bulletin.


#include <stdio.h>
int get_cycle_length(int n);

int main() {
    int i, j;
    int l, r;
    int max;
    int n;
    int cycle_length;
    char line[10000] = { 0 };
    size_t line_len = sizeof(line), read_len = 0, pos = 0;
    while (scanf(" %d%d", &i, &j) != EOF) {
        l = i > j? j: i;
        r = i > j? i: j;
        max = -1;
        for (n = l; n <= r; n++) {
            cycle_length = get_cycle_length(n);
            if (cycle_length > max)
                max = cycle_length;
        }

        read_len = snprintf(line+pos, line_len-pos, "%d %d %d\n", i, j, max);
        if (pos+read_len > line_len) {
            printf("%.*s", (int)pos, line);
            pos = 0;
        }
        pos += read_len;

        // printf("%d %d %d\n", i, j, max);
    }
    printf("%.*s", (int)pos, line);
    return 0;
}

int get_cycle_length(int n) {
    int count = 0;
    while (n != 1) {
        count++;
        n = n & 1? 3*n+1: n/2;
    }
    count++;
    return count;
}
5
  • 2
    And what are errors? Commented Feb 11, 2021 at 18:40
  • @VladfromMoscow: I'm using VS2019 and I got no error, but after submission it only shows CE. Commented Feb 11, 2021 at 18:41
  • On first glance I would suspect this line: ` char line[10000] = { 0 }; `
    – Devolus
    Commented Feb 11, 2021 at 18:44
  • @Devolus: I've tried move it to global and omit the = { 0 }, still got CE. Commented Feb 11, 2021 at 18:52
  • 2
    Try replacing the // comment with /* ... */.
    – dbush
    Commented Feb 11, 2021 at 18:52

1 Answer 1

3

You're using C++ style comments in your code, i.e. //.

Many C compilers will allow these types of comments as an extension, however strict ANSI C does not. You'll need to use C style comments /* ... */.

So instead of this:

// printf("%d %d %d\n", i, j, max);

Do this:

/*
printf("%d %d %d\n", i, j, max);
*/
1
  • Lol, I thought it was (and have tried them) size_t, or local char line[], missing #include<...> (because IDE always help me too much..., i.e. auto-include behind the scene). Commented Feb 11, 2021 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.