Compile and run C Program in Linux or Unix | Compiling C program using GCC
Compiling C program on Linux ( Ubuntu, CentOS, Fedora )
Here are the steps for Compiling C program under GNU Linux system.
- Create a source file.
- Compile the source file.
- Run the Executable file.
Create a Source File:
First of all create a source file by using your favourite editor. I am using vim editor and let’s write a simple hello world program.
1 |
vim helloworld.c |
1 2 3 4 5 6 |
#include<stdio.h> int main() { printf("Hello World \n"); return 0; } |
Compiling C Program:
All Linux systems comes with the GCC compiler. GCC stands for GNU compiler Collection. We can use GCC compiler to compile the C programs in Linux based systems like Ubuntu, Centos, Debian, etc.
To compile the program using the gcc, Use the following command.
1 |
gcc helloworld.c |
Note here helloworld.c is our file name.
The above command will generate a Executable file or Binary file. Which we can run later. As we are working with Linux based system, The default executable file name is a.out
Check your present working directory for the file named a.out, That is our executable file.
GCC compiler also have option to specify the resulting executable file. If you want to give a specific name for your executable file use option -o while compiling the program.
1 |
$ gcc <source_filename> -o <executable_file_name> |
Running the C Program :
Now we got executable file, To run the executable file just type the executable filename, here executable is a.out So we need to run
1 |
$ ./a.out |
Here ./ (back-slash) present directory. or you can use full pathname like /home/venkey/a.out to run your program.
Bonus : Few GCC Tips:
There are few Interesting options are available in GCC have a look.
If you know different stages of C program compilation then you might know about preprocessor, translator and the assembler and linker. GCC provides options to stop compilation at above-specified stages.
Recommended :
You can read Different Compilation stages of C program.
Let’s say we have one source file and filename of the source file is prog.c
To get pre-processed code use option -E.
1 |
$ gcc –E prog.c –o prog.i |
To get translated code use option -S.
1 |
$ gcc –S prog.i –o prog.s |
To get Assembled code use option -c (note c is small alphabet)
1 |
$ gcc –c prog.s –o prog.o |
Here prog.o is object file.
To get the executable file just run the object file.
1 |
$ gcc prog.o |
Now we got the executable file a.out you can run this one to see the output.
Also Check:
- Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes
- Come Together – The Collection of all Pattern Programs in C – SillyCodes
Related Articles :
- C Program to calculate area and perimeter of Rectangle.
- C Program to calculate area of Traingle.
- C program to generate Prime Numbers between n1 and n2.
- C Program to Generate Fibonacci series using Recursion.
- More Mathematical C Programs.
- 200+ C Language programs.
14 Responses
[…] Compile and run C Program in Linux or Unix | Compiling C program using GCC […]
[…] https://sillycodes.com/compiling-c-program-in-linux-or-unix/ […]
[…] Compile and run the program. We are using the GCC compiler under the Linux Operating system to Run and compile the program. […]
[…] Compile and run C Program in Linux or Unix […]
[…] 📢 Learn More – Compiling and running C Program using GCC on Linux Operating System […]
[…] 📢 Compiling C Programs in Linux using the GCC compiler. […]
[…] 📢Compiling and Run C Program using GCC compiler […]
[…] the program using your favorite compiler. We are using the GCC compiler […]
[…] compile and Run the program using your favorite compiler. We are using the GCC compiler on Ubuntu […]
[…] Compile and Run the program using GCC (Any Compiler). Here are the instructions to compile and run the program using the GCC compiler. […]
[…] Here are the instructions to Compile and Run the C Programs in Linux. […]
[…] 📢 Instructions to Compile and Run C Program using GCC compiler on Ubuntu Linux […]
[…] 📢Instructions to Compile and Run Program on Ubuntu Linux using GCC compiler. […]
[…] Let’s compile the program using your favorite compiler. Here we are using the GCC compiler. […]