CodeBlanks Logo Icon CodeBlanks

Compile and run

Learn to compile and run a C program with gcc.

Compiling with gcc

To run a C program you first need to compile it. The most common compiler is gcc. To compile hello.c into an executable named hello:

gcc hello.c -o hello

Then run it with ./hello. The -o hello part sets the output file name; without it the default name is a.out.

Compilation stages

Compilation actually has four stages:

  1. Preprocessing: #include and #define are expanded
  2. Compilation: code is turned into assembly
  3. Assembly: assembly is turned into machine code
  4. Linking: external libraries are added

gcc runs all stages with a single command.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which command produces an executable?

  • execute hello.c
  • run hello.c
  • gcc hello.c -o hello Correct answer
  • compile hello.c

Explanation: gcc hello.c -o hello compiles hello.c into an executable named hello.

2 Output file flag

Question
gcc hello.c ____ hello
Answer
gcc hello.c -o hello

Explanation: -o sets the output file name.

3 Match the commands

Term Meaning
gcc Compiler
./hello Run
hello.c Source file
a.out Default output

4 What is produced by 'gcc main.c'?

  • main.exe
  • program
  • main.class
  • a.out Correct answer

Explanation: Without -o, gcc names the output a.out.

All lessons in Beginner