-4

Hey all. I wrote this program for an online contest. It RUNS FINE on Bloodshed's Dev-C++ but it breaks down with GCC. The contest demands all solutions based on GCC compiler.

It doesn't even wait for the 2nd input. The program worked fine on Dev-C++. Please, help.

#include <stdio.h>

int testCases;

void runPgm()
{
    scanf("%d", &testCases);
    int array[testCases][2];
    char start[testCases];
    int i;
    for(i=0; i<testCases; i++)
    {
        scanf("%c %d %d", &start[i], &array[i][0], &array[i][1]);
    }
    for(i=0; i<testCases; i++)
    {
        printf("%c %d %d\n", start[i], array[i][0], array[i][1]);
    }
}

int main() {
    runPgm();
    return 0;
}

Output with GCC:

machine:~$ cc prog.c -lm  
machine:~$ ./a.out  
2
a 3 3

 0 -1080493616
a 3 3
machine:~$
jweyrich
  • 31,198
  • 5
  • 66
  • 97
letsc
  • 2,075
  • 5
  • 24
  • 43

1 Answers1

1

After getting "testCases", you use the "Enter" key, which adds "\n" to the buffer.

You should use getchar() to get the "\n" out of the buffer. same thing for the scanf in the for loop

Your fixed code:

#include <stdio.h>
int testCases;


void runPgm()
{
    scanf("%d", &testCases);
    getchar();

    int array[testCases][2];
    char start[testCases];
    int i;
    for(i=0; i<testCases; i++)
    {
        scanf("%c %d %d", &start[i], &array[i][0], &array[i][1]);
        getchar();
    }
    for(i=0; i<testCases; i++)
    {
        printf("%c %d %d\n", start[i], array[i][0], array[i][1]);
    }
}

int main() {
    runPgm();
    return 0;
}

BTW, defining arrays like you did, is not ANSI-C compatible, and I am not sure why gcc is ok with this. You should use dynamic allocation for that purpose (like malloc())

  • Regarding the last paragraph, it's supported by C ANSI, and is known as variable-length array (VLA). But you/he should pass testCases by parameter to `runPgm`. +1 nevertheless. – jweyrich Feb 13 '11 at 15:40
  • @jweyrich, why and how should he pass this as a parameter? It is not known on entry to the function. I agree that working though a global is not the best of a design, but there is nothing that forces this. – Jens Gustedt Feb 13 '11 at 17:48
  • @Jens: because the standard requires variables to be declared at the start of a block. GCC allows it due to an extension. Just move the 1st `scanf`+`getchar` to `main`, add an `int` parameter to the function prototype, and pass it accordingly. Alternatively, he could just wrap the arrays declaration plus the following code in a new block. – jweyrich Feb 13 '11 at 18:16
  • @jweyrich, *the* actual C standard is C99, out since 12 years now, and it allows to declare variables anywhere before their first use. And BTW, he declares that variable as a global, this has been allowed for all times in C. – Jens Gustedt Feb 13 '11 at 18:22
  • @Jens: I'm referring to the arrays, not the global variable. Yep, C ANSI = C99 = actual standard. I always thought mixed code & decls were an extension, but I just checked the standard and I'm wrong. Sorry for the confusion :-) – jweyrich Feb 13 '11 at 18:27
  • @jweyrich: yes it does, but many people seem to be reluctant to it, though. And it also allows for `for`-loop variables. – Jens Gustedt Feb 13 '11 at 18:34