The first time to compile a C program may be daunting–it may be a Unix environment where commands, files and terminal operations are unknown. This is however one of the most empowering experiences of any novice programmer. As soon as you have the knowledge of how to translate a simple C file into an executable program, you have the key to the world of systems programming, embedded development, operating systems work, and the myriad other areas of computing.
This article takes you through the process step-by-step: how to write your first C program, how to save this program, how to know about compilers such as GCC, how to compile your program, how to run it and how to trouble shoot the most common beginner mistakes. All that is clarified in a simple practical and beginner-based manner so that you can develop confidence instantly.
To help you learn even more, here is a resource to aid in your learning of what a c program is and how the language functions at the most simple level.
Table of Contents
- A C Programming on Unix Introduction.
- What You Need Before You Start.
- How Unix Handles Programs
- Writing Your First C Program
- Program Correction Savings.
- Getting acquainted with GCC: The GNU Compiler Collection.
- Step-by-Step: Preparing a C Program.
- Running the Executable File
- Mistakes to avoid and the ways out.
- Commenting and Increasing Code Readability.
- Learning the Compilation Stages.
- Execution Rights and file permissions.
- Practical Sports Beginner and Intermediate.
- Tips for Debugging
- Conclusion
Introduction to Unix C Programming
C and Unix were created simultaneously. True to say, Unix was first rewritten in C in the early 1970s, and it is this combination that computer science has known ever since. Unix offers a highly effective and consistent platform to operate the C language, with the developers having full control over the compile-run cycle.
Unix systems (such as Linux or macOS terminals) can be uncontemplated to newcomers, as everything seems to be run using text commands. These commands are however very logical when disaggregated. Once you get your first C program to compile successfully, you will think of the beauty of the Unix ecosystem clean, controllable, and developer-centered.
What You Need Before You Begin
Prior to starting, you must have:
One of the Unix-based Operating Systems
This includes:
- Distributions based on Linux (Ubuntu, Fedora, Debian, Mint, etc.).
- macOS (has Unix roots)
- Unix servers or cloud terminals (e.g. SSH access to a Linux server)
A Terminal
All the commands will be typed on the terminal.
A Text Editor
Unix has numerous editors, including:
- nano (beginner-friendly)
- vim (fast, but requires education)
- emacs
- gui editors such as Vs code (not mandatory, but useful)
A C Compiler: GCC
GCC is usually pre-installed with most Unix systems or it can be installed with ease.
To verify the existence of GCC, write:
gcc –version
When you get a version number, then that is fine.
Otherwise, one can install GCC (under Linux):
sudo apt-get build-essential.
or on MacOS (needs Xcode command-line tools):
xcode-select –install
How Unix Handles Programs
The way Unix treats code to know about compilation:
- C file (.c) — your source code
- Compiler – translates machine code to source code.
- Object file (.o) – compiled code.
- Execution code: a runnable code.
Unix terminal coordinates these steps by use of simple commands. This may seem to be a technical process, but all the steps are foreseeable.
Writing Your First C Program
We will begin with the most basic one: a Hello, World! program.
Open your terminal and type:
nano hello.c
This executes the Nano editor of a new file called hello.c.
Now type the following C code:
#include <stdio.h>
int main() {
printf(“Hello, Unix world!\n”);
return 0;
}
Here’s what each line means:
- importing the standard input/output library.
- int main: the beginning of the program.
- printf: will output any text to the terminal.
- return 0: informs the operating system that the program has been run successfully.
This small program already illustrates the style of all C applications.

Savings the Program in The Right Way
If you’re using Nano:
- Press CTRL + O to save
- Press ENTER to confirm
- Press CTRL + X to exit

If using Vim:
- Press Esc
- Type :wq
- Press Enter
You have saved your file hello.c in your current directory.
You can check it using:
ls
Your file should be listed.
Introduction to GCC: The GNU Compiler Collection
A widely used compiler in the world is GCC (Gnu Compiler Collection). It converts human readable C code into machine instructions that are comprehended by the CPU.
Why GCC is important:
- Free and open-source
- Very portable (works with virtually any system)
- Reliable and fast
- Several languages (C, C++, Fortran, etc.)
- Develops wise tips to starters.
When you compile a C program, what you are telling GCC is to translate your .c file into an executable file.
Step-by-Step: Building a C Program
To compile hello.c, run:
gcc hello.c -o hello
Here’s what each part means:
- gcc: the compiler command
- hello.c: your source file
- -o hello: output file (your final program) name.
In case the command is completed successfully, there will be no message. This is normal.
To see your new executable you can list files:
ls
Now you should see:
- hello.c
- hello
Running the Executable File
To run your program:
./hello
You should see:
Hello, Unix world!
This is when you will start your C programming life-cycle when you will see your own code running.
Mistakes and the Ways to overcome them
Error 1: Missing Semicolon
Fix: Add semicolon.
Error 2: Typing errors in Function Names.
Fix: Check spelling.
Error 3: Missing Header File
Fix: Add missing header.
Error 4: Saving File of the incorrect extension.
Fix: Always use .c.
Error 5: Running Denied Permission.
Fix:
chmod +x hello
Commenting and Making Code more Readable
Good code is readable code.
Single-line comment
// This displays a greeting statement.
Multi-line comment
/*
In this program, a message is printed.
It is of learning simple compilation.
*/
Commenting helps:
- Late you know your own code.
- Your intention is comprehended by others.
- Debugging becomes easier
The Compilation Stages: An Overview
GCC in fact undertakes four significant stages:
Preprocessing
gcc -E hello.c
Compilation
gcc -S hello.c
Assembly
gcc -c hello.c
Output: hello.o
Linking
gcc hello.o -o hello
The knowledge of these stages assists in debugging and optimization.
Execution Rights and File Permissions
To view permissions:
ls -l
You might see:
-rwxr-xr-x 1 user user 8544 hello
This breaks down to:
- r = read
- w = write
- x = execute
If your file has no execute permission:
chmod u+x hello

Examples in Practice: Introduction
Example 1: Requesting User input.
#include <stdio.h>
int main() {
int age;
printf(“Enter your age: “);
scanf(“%d”, &age);
printf( you are age years old);
return 0;
}
Example 2: Simple Arithmetic Program
#include <stdio.h>
int main() {
int a, b;
printf(“Enter two numbers: “);
scanf(“%d %d”, &a, &b);
printf(“Sum = %d\n”, a + b);
printf(“Difference = %d\n”, a – b);
printf(“Product = %d\n”, a * b);
return 0;
}
Example 3: Using Loops
#include <stdio.h>
int main() {
for(int i = 1; i <= 5; i++) {
printf(“Iteration %d\n”, i);
}
return 0;
}
Tips for Debugging
- Add Print Statements
- Enable Warnings:
gcc -Wall hello.c -o hello - Step-by-Step Inspection using gdb
- Re-check File Paths
- Check Semicolons, Braces, Parentheses
Conclusion
You have now learned the entire procedure of writing, saving, compiling and executing your first C program within a Unix. you have also learned the GCC functionality, compilation stages, how to debug, and testing your code using real life examples.
It is not as hard as it might have appeared in the beginning after you divide it into steps. Unix terminal is one of the most effective tools that a programmer can work with and combining it with the C language gives you complete power over what your computing environment is going to be like- not only running your programs but also running your system.
Knowing these fundamentals will ready you to do more advanced programmation, algorithms, data structures, and even systems engineering. Keep on trying and experiment with more complicated programs and most importantly, always bear in mind, all great programmers started with a simple hello.c.