How to write compile and run a C Program on Linux command line

On Linux systems you do not need to have an IDE for C programming, means you can just write, compile, and run a C Program using Linux command line.

Though there are a lot of IDE software available for C Programming on Linux platforms like Code-blocks, Net-beans, Eclipse etc, still many people find the command line more convenient. So let us check how to do that.

Let us get started

You might have seen a command prompt on Windows systems, where you can type commands for system administration or play DOS games. Similarly, a Linux system command prompt is called a Linux terminal. Linux terminal is a powerful tool for Linux users to play with files and folders, system administration and coding.

Here I am using Ubuntu 20.04 Linux system for this tutorial, but these steps would almost work on all Linux distributions.

Open Linux terminal

Step -1 By using keyboard shortcut keys

Pressing ‘CTRL+Alt+T’ keys simultaneously on the keyboard will open the Linux terminal on your system.

Step -2 By using Graphical User Interface

On a Linux Gnome desktop pressing a Super key (also called Window key) will open the applications list, open the ‘Terminal‘ application from here.

Linux Terminal

On other Linux desktop environments, you have to search ‘Terminal’ in the applications list.

Writing a C Program

Step -1 Open text editor

Now to write a C Program, you need to have a text editor. There are many built in text editors for Linux systems e.g., Vi, Vim, Emacs and Nano etc. For the graphical environment I prefer to use ‘gedit’ and on the command line ‘nano’ is the best text editor.

So here, I am going to use the simplest text editor called ‘nano editor’. It has all commands listed on the bottom screen, so you do not need to memorize commands to interact with files.

Type below command on your Linux terminal.

nano helloworld.c

nano helloworld.c

Step -2 Type C program in nano editor

You can start writing your C Program in nano editor just after opening it.

C Program in nano editor

Step -3 Saving C program in nano editor

After you completed typing your C Program in nano editor, save the program using keyboard shortcut keys 'CTRL+O' and press enter.

saving c program in nano editor

After your program is saved successfully, again it will show a success message.

Step -4 Exiting nano editor

After that press 'CTRL+X' keys simultaneously to exit out of the nano editor.

exiting nano editor

In this way you have successfully opened an empty C Program file. Then you wrote a C Program, saved the file, and exited out of nano editor.

Compile C Program

After closing nano editor, you will be back to the command line at $ prompt. Now check if your new file exists or not and display its content using the ‘ls’ and ‘cat’ commands as shown in screenshot below.

Step -1 Using GCC command 

Now you are ready to compile your C Program using GCC compiler, enter the below command on the terminal.

gcc -o helloworld.out helloworld.c

compiling c program using gcc

After entering this command, if the program has no errors, then it will create an executable C Program output file. In this example ‘helloworld.out’. Otherwise, first you must remove the errors of your C Program, save the program file and then run this command again to compile your program until the program is error free.

Step -2 Running C Program output

Now to run the executable C Program output enter the following command.

./helloworld.out

running a c program output

Note: - After compiling your C Program, there are two files now,

  • C Program file e.g., “helloworld.c”
  • C Program output file e.g., “helloworld.out”

Conclusion

In this article, we have seen how to write, compile and run a C Program using Linux command line. We wrote a simple C Program using nano editor, then compiled it using GCC compiler on Linux command line.

After C Program compilation, we get a executable C Program output file. And finally we have seen how to execute or run the C Program output.

Comments