Back to C Programming
2026-07-126 min read

Compiling a C Program

Learn Compiling a C Program step by step with clear examples and exercises.

Why This Matters

In this full guide, we'll explore the critical process of compiling a C program from scratch. Mastering compilation is key to transforming your high-level code into machine-executable instructions, enabling efficient execution and debugging. This skill set will empower you to optimize programs, troubleshoot errors, and ultimately, become a more proficient C programmer.

Prerequisites

Before diving into compiling a C program, ensure you have the following prerequisites:

  1. A text editor (such as Visual Studio Code or Notepad++) to write your code.
  2. A C compiler installed on your system. On most Linux distributions, this is already installed by default. For Windows users, you can download the MinGW-w64 GCC compiler from here.
  3. Familiarity with basic C syntax, such as variables, functions, loops, and control structures. If you're new to C programming, we recommend checking out our C for Beginners guide.
  4. A basic understanding of the file system and how to navigate it using a terminal or command prompt.

Core Concept

To compile a C program, follow these steps:

  1. Write your code in a text editor and save it with a .c extension (e.g., my_program.c).
  2. Open a terminal or command prompt and navigate to the directory containing your source file.
  3. Type the following command to compile your program:
gcc my_program.c -o my_program

This command tells the GCC compiler (gcc) to compile the my_program.c file and generate an executable named my_program.

  1. After the compilation process is complete, you can run your program by typing:
./my_program

Compiler Options

The GCC compiler supports various options to control the compilation process. Some common options include:

  • -o: Specifies the output file name (e.g., -o my_program).
  • -Wall: Enables all warning messages during compilation.
  • -Werror: Treats warnings as errors, causing the compiler to stop if any warnings are encountered.
  • -g: Generates debugging information for use with a debugger.
  • -std=c99 or -std=c11: Specifies the C standard version used during compilation (C99 by default).
  • -I path: Includes the specified directory when searching for header files.
  • -L path: Adds the specified library directory to the linker's library search path.
  • -l library: Links the specified library during the linking phase (e.g., -lstdc++ for the C++ standard library).

Worked Example

Let's compile and run a simple C program that calculates the factorial of a number entered by the user.

  1. Open your text editor and create a new file called factorial.c.
  2. Paste the following code into the file:
#include <stdio.h>

long long int factorial(int n);

int main() {
int num;

printf("Enter a positive integer: ");
scanf("%d", &num);

if (num < 0) {
printf("Error! Factorial of negative numbers is not defined.\n");
return 1;
}

long long int result = factorial(num);
printf("The factorial of %d is: %lld\n", num, result);
return 0;
}

long long int factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
  1. Save the file and navigate to its directory in your terminal or command prompt.
  2. Compile the program using the following command:
gcc -o factorial factorial.c
  1. Run the compiled program with this command:
./factorial
  1. Enter a positive integer when prompted, and you should see the factorial of that number printed to the console.

Common Mistakes

Missing Semicolon

One of the most common mistakes beginners make is forgetting to include a semicolon at the end of a statement. For example:

int main() {
int x;
printf("Enter a number: ");
scanf("%d", x); // missing semicolon here
}

To fix this error, simply add a semicolon after the variable declaration or before the printf function call:

int main() {
int x;
printf("Enter a number: ");
scanf("%d", &x); // added semicolon here
}

Missing Include Statements

Another common mistake is forgetting to include necessary header files. For example, if you try to use the printf function without including the stdio.h header:

#include stdio.h // incorrect syntax

int main() {
printf("Hello, World!");
}

To fix this error, ensure you include the correct header file:

#include <stdio.h>

int main() {
printf("Hello, World!");
}

Missing Function Prototypes

If a function is defined after it's called in the same source file, you must provide a function prototype before the first call to that function. For example:

int main() {
int result = sum(2, 3); // calling the sum function before its definition

int sum(int a, int b); // function prototype here
}

int sum(int a, int b) {
return a + b;
}

To fix this error, move the function prototype before the first call to the sum function:

int sum(int a, int b); // function prototype here

int main() {
int result = sum(2, 3);

int sum(int a, int b) {
return a + b;
}
}

Practice Questions

  1. Write a program that calculates the sum of an array of numbers entered by the user.
  2. Implement a function that finds the maximum element in an array.
  3. Create a simple program that uses pointers to swap the values of two variables without using a temporary variable.
  4. Write a program that reads a line of text from the user and counts the number of vowels, consonants, and whitespace characters in it.
  5. Implement a recursive function that finds the Fibonacci sequence up to a given number.
  6. Create a program that sorts an array of integers using bubble sort.
  7. Write a program that generates and prints the first n prime numbers, where n is entered by the user.
  8. Implement a function that finds the smallest common multiple (SCM) of two numbers using Euclid's algorithm.
  9. Create a program that calculates the factorial of a number using recursion and dynamic memory allocation to store intermediate results.
  10. Write a program that reads a file containing integers, sorts them in ascending order, and writes the sorted list to another file.

FAQ

Q: Why does my compiler output warnings when I compile my code?

A: Warnings indicate potential issues with your code that may cause unexpected behavior or errors at runtime. It's essential to address these warnings to ensure your program runs correctly and adheres to good programming practices.

Q: How can I debug my C program if it doesn't run as expected?

A: You can use a debugger, such as GDB (GNU Debugger), to step through your code line by line and inspect variables at each step. To learn more about using GDB, check out our C Debugging with GDB tutorial.

Q: What is the difference between a header file and a source file in C programming?

A: A header file (e.g., stdio.h) contains function declarations and macro definitions that can be included in multiple source files to ensure consistency and avoid redundancy. A source file (.c) contains the actual implementation of functions and variables for a specific program or module. Header files usually have the extension .h, while source files have the extension .c.

Q: How do I link my C program with external libraries, such as SDL or OpenGL?

A: To link your C program with an external library, you need to specify the library during the compilation process using the -l option followed by the library name (e.g., -lSDL or -lOpenGL). If the library is located in a non-standard directory, you may also need to use the -L option to specify its path. For example:

gcc -o my_program my_program.c -L /path/to/library -lmy_library