Skip to content

Compiling and running code

Build an executable from our source code and run it.

We can compile our C program using a program known as gcc.

First, open a terminal and make sure you are in the directory which contains your C source file (e.g. main.c). If not, use the cd command to get there.

After that, run this command:

Terminal window
gcc main.c

(replacing main.c with the name of your C source file).

Assuming you are on a Unix-based system or are emulating a Unix system, gcc will produce a binary called a.out. You can run your program using this command:

Terminal window
./a.out

Sometimes, gcc can output a file marked without execute permissions. This means that you will not be able to run your program unless you change the file permission attributes. You can fix that with a simple command:

Terminal window
chmod +rwx a.out

gcc allows us to choose the name of the binary file it outputs (instead of a.out). We can do this by specifying the -o flag when compiling:

Terminal window
gcc main.c -o outputbinary

This will compile our program into the outputbinary file.

Then, to run the outputbinary program:

Terminal window
./outputbinary

We can compile the source file and execute the binary in a single command. The command below will compile the code and only if is successful, will execute the program:

Terminal window
gcc main.c && ./a.out

If you renamed your program’s output binary, say, to mycoolprogram, you would use:

Terminal window
gcc main.c -o mycoolprogram && ./mycoolprogram