How To Write and Run C Program in Linux
Learning how to write and run a C program in Linux is an essential skill for anyone interested in system programming or software development. The C programming language has been a cornerstone of computing for decades, and it’s still widely used today for developing operating systems, embedded systems, and high-performance applications. In this guide, you’ll discover the step-by-step process of writing, compiling, and running a simple C program on a Linux system. Whether you’re a beginner or an experienced developer looking to refresh your skills, this tutorial will walk you through everything you need to know.
Prerequisites for Writing and Running C Programs in Linux
Before you begin writing your first C program in Linux, there are a few prerequisites you should be aware of. These include basic knowledge of the command-line interface (CLI) and familiarity with the syntax of the C programming language.
Tools You Need
- Text Editor: You can use any text editor to write your C code. Popular options include CLI-based editors like
nano
orvim
, as well as GUI-based editors likegedit
or Visual Studio Code. - GCC Compiler: The GNU Compiler Collection (GCC) is the most commonly used compiler for compiling C programs on Linux.
Installing GCC Compiler
If GCC is not already installed on your system, you can easily install it using your package manager. For Debian-based distributions like Ubuntu, use the following command:
sudo apt install build-essential
This command installs GCC along with other essential tools for building software. For Fedora or Red Hat-based distributions, use:
sudo dnf groupinstall "Development Tools"
Once installed, you can verify that GCC is available by typing:
gcc --version
If the installation was successful, you should see the version number of GCC displayed.
Step-by-Step Guide to Writing a Simple C Program in Linux
1. Open a Text Editor
The first step is to open a text editor to write your code. If you’re comfortable with the command line, you can use nano
, vim
, or any other CLI-based editor. For example:
nano hello.c
This command opens the nano editor with a new file named hello.c
. If you prefer using a graphical interface, you can open gedit
, Visual Studio Code, or any other GUI-based editor.
2. Write the C Program
Now that your text editor is open, write a simple “Hello World” program in C:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
This basic program prints “Hello, World!” to the terminal when executed. Save the file with the extension .c, which indicates that it’s a C source file.
Compiling the C Program Using GCC Compiler
1. What is GCC?
The GNU Compiler Collection (GCC) is one of the most widely used compilers for compiling programs written in languages like C and C++. In this tutorial, we’ll be using it to compile our simple “Hello World” program.
2. Compile the Program
The next step is to compile your code using GCC. Open your terminal and navigate to the directory where your source file is saved. Then run the following command:
gcc hello.c -o hello
This command tells GCC to compile hello.c, and the -o hello flag specifies that the output should be an executable file named hello. If there are no errors in your code, GCC will generate an executable file.
Troubleshooting Compilation Errors
If you encounter any errors during compilation, carefully read the error messages provided by GCC. Common issues include missing semicolons or undeclared variables. For example:
- Error: expected ‘;’ before ‘return’: This error indicates that you’ve forgotten to add a semicolon at the end of a statement.
- Error: ‘printf’ was not declared: This error usually occurs if you’ve forgotten to include the necessary header files like
#include <stdio.h>
.
Running the Compiled C Program in Linux
1. Execute the Program
You can now run your compiled program by typing:
./hello
The output should display:
Hello, World!
This confirms that your program has been successfully compiled and executed.
Add Executable Path (Optional)
If you’d like to avoid typing ./hello, you can add your current directory to your system’s PATH environment variable by modifying your shell configuration file (e.g., .bashrc):
export PATH=.:$PATH
This allows you to run executables from the current directory without needing to prefix them with “./”. After modifying this file, reload it using:
source ~/.bashrc
Using Integrated Development Environments (IDEs) or Code Editors for Running C Programs
The Benefits of Using IDEs for Larger Projects
If you’re working on larger projects or prefer a more user-friendly interface for writing code, using an Integrated Development Environment (IDE) might be more efficient than working directly from the terminal. IDEs such as Visual Studio Code or Eclipse provide features like syntax highlighting, debugging tools, and project management capabilities.
Setting Up Visual Studio Code for C Programming on Linux
If you’re using Visual Studio Code (VS Code), you’ll need to install some extensions to support C programming. Follow these steps:
- C/C++ Extension Pack: Install this extension from Microsoft via VS Code’s marketplace.
- C/C++ Compile Run: This extension allows you to compile and run programs directly within VS Code.
- CMake Tools (Optional): If you’re working on more complex projects with multiple files.
Running Programs in VS Code Using Shortcuts
You can easily compile and run programs within VS Code by pressing `Ctrl+Alt+N
` after installing relevant extensions. This shortcut will automatically compile and execute your code without needing terminal commands manually.