Executing C program in Unix/Linux terminal

First you need to check whether gcc compiler is installed in your system. To check that use the command.
gcc --version

If it has already been installed, you can skip the below one step and go to "Steps to create and run a C file in Ubuntu"

In case if it has not been installed you can install it by following the below commands:
1. sudo apt update
2. sudo apt install build-essential

Steps to create and run a C file in Ubuntu:
1. Open terminal by pressing CTRL + ALT + T
2. Type gedit hello.c
The above command will open your gedit text editor (default text editor in Ubuntu) and creates a new file named hello.c
3. Now hello.c has been opened in your gedit text editor. Type the below code and save it

#include<stdio.h>
void main(){
printf("Hello!");
}


4. Now your file has been saved. Still you need to compile it for running the program. Open terminal and type gcc hello.c
5. Now a new file named a.out will be save in your current directory.

6. So compilation has been done and itself saved as a.out. Then we need to run the compiled file not the c file.
7. To run the program type ./a.out in terminal. Make sure to stick on to the directory of the file while using terminal.
8. Now your program output will be displayed in Ubuntu terminal.

You can also make a compiled file of your preferred name using the following command gcc -o hello hello.c
The above command creates the compiled file named hello from the program file hello.c

Comments