Search code examples
cgccmingw

How to run C code/file with MINGW?


OK, I just began learning C language, and I read a lot of people suggest to use MinGW as a compiler. I downloaded, installed it and set it correctly (I think) and set the path. everything seems to be OK. My problem is that I can't find a way to run a C file from the CMD, when I do something like this:

C:\>gcc a\c1.c

nothing appear on the CMD

In c1.c file I have the following:

#include <stdio.h>

int main(void)
{
    printf("this is a test.\n");
    return 0;
}

I tried tcc compiler and when using it this way:

C:\>tcc a\c1.c

it outputs the following:

this is a test.

How can I make MinGW output that the same way? I looked in the MinGW/gcc manual and HowTos and couldn't find a way to do the same thing.


Solution

  • gcc filename.c compiles and links your file -- but it doesn't write the executable to hello.exe as you might expect (and of course it doesn't execute it).

    And, as is the convention for Unix-based tools, gcc generally doesn't print anything unless something went wrong. The fact that it finished and you got a new C:\> prompt is what tells you that it succeeded.

    On Unix, for historical reasons, the default name of an executable is a.out (it stands for "assembler output"). For MinGW, the default name is a.exe, since Windows requires a .exe suffix for executable files.

    So after running gcc a\c1.c, you can run a.exe or .\a.exe to run the program.

    Or, better, as Doug suggests, you can use the -o option to specify the name of the generated executable file.

    (If you want to run just the compiler and generate an object file rather than an executable, use gcc -c.)

    Note that this behavior (not printing anything unless there's a problem, naming the executable file a.out or a.exe, etc.) is defined by the gcc compiler in particular. It's not a feature of the C language. Other C compilers might or might not behave similarly.