How to Execute a C Program in Linux
If you are new to programming or have recently ventured into the world of Linux, executing a C program might seem like a daunting task. However, with the right guidance, you can easily compile and run your C programs on a Linux system. In this article, we will walk you through the steps to execute a C program in Linux, from installing the necessary tools to compiling and running your code.
Step 1: Install a C Compiler
The first step in executing a C program in Linux is to install a C compiler. The most commonly used compiler is GCC (GNU Compiler Collection). To install GCC, open your terminal and type the following command:
“`
sudo apt-get install build-essential
“`
For CentOS or RHEL, use the following command:
“`
sudo yum groupinstall “Development Tools”
“`
For Fedora, use:
“`
sudo dnf groupinstall “Development Tools”
“`
Once the installation is complete, you can verify that GCC is installed by typing:
“`
gcc –version
“`
Step 2: Write Your C Program
Next, you need to write your C program. Create a new file using a text editor, such as `nano`, `vim`, or `gedit`. For this example, we will create a simple “Hello, World!” program named `hello.c`:
“`
include
int main() {
printf(“Hello, World!”);
return 0;
}
“`
Save the file and exit the text editor.
Step 3: Compile Your C Program
Now that you have your C program written, you need to compile it using the GCC compiler. In the terminal, navigate to the directory containing your `hello.c` file and type the following command:
“`
gcc hello.c -o hello
“`
This command tells GCC to compile `hello.c` and create an executable file named `hello`. The `-o` option specifies the output file name.
Step 4: Execute Your C Program
After compiling your C program, you can execute it by typing the following command in the terminal:
“`
./hello
“`
If everything is set up correctly, you should see the output “Hello, World!” printed to the terminal.
Conclusion
Executing a C program in Linux is a straightforward process, as long as you have the necessary tools installed and follow the right steps. By following the instructions in this article, you should now be able to compile and run your C programs on a Linux system. Happy coding!