Compile C program using gcc in Linux

“This post explains about using gcc to compile C program on Linux”

Compile  C program using gcc:

  • What is a compiler:

Compiler is just like translator between programing language and machine language. It converts source written in programing language to executable instructions file for computer. For different programing languages different compilers are available. Compilers differs from operating system to operating system.

  • Open text editor:

Compiling C program start with a text editor to write our C program like VI . It is generally inbuilt in Linux operating systems. By opening terminal in our system we start from there.screenshot-from-2016-12-05-13-14-11





  • Write code:

In terminal type: vi sample.c  and Enter key. Then we enter in to vi text editor with our filename given. Now type a or i key to go in to insert mode. Then type our C program in it. After typing C program press Esc key,colon(:),w,q and Enter key  respectively to save and exit from VI. Here is figure showing source code.


  • Compile using gcc command:

Now we are in terminal again.Here we type ls command to see our saved file in the list. Then type gcc sample.c -o sample and enter key. Now the gcc compiler compiles our C file and gives the output as filename we given that is executable. screenshot-from-2016-12-05-13-20-29

  • Execute output file:

If there are any mistakes or errors in the program the compiler gives warnings and error messages with line number to find out them easily, after correcting them compile once again. If it compiles successfully it gives executable file.  To check that file we use ls command and see if it is. If it is, now type ./sample in terminal to execute it. we see the result on terminal.screenshot-from-2016-12-05-13-22-54

Actually in compiling process preprocessor adds the necessary files those are included in C libraries. That we are listed in first of our program like <stdio.h> and some other files also generated by compiler . One of  those file is object file.

Let Us C.

  • How it happened:

As usual  one Sunday I am going to Church. I met one of my friend there. In general conversation he asked about me what you are doing? I told him that I am learning C-language. Then he suggested a book for learning C-language. Which he already read in his studies B.Tech. Then I asked the title of that book

  • What is that book?

He said that is “Let Us C” written by” Yaswant Kanetkar“.  It is good for me.

There is another book, that is “C The Programming Language” by Brian W. Kernighan and Dennis M.Ritchie . First I am started learning C with this book. The content in it is compatible with Linux, which is referred by One of my well – wisher.

  • Are there exercises in “Let Us C”?

In this book” Let Us C” they have given some exercises at the end of every chapter. But first of all I am reading the book chapter wise, some times I copied the source code in to “vi” text editor, compiled using gcc and getting output on a Linux system which is having Ubuntu 16.04

    • My suggestion:
      So I suggest it for those who are like me in learning C-language.
  • Regards:

I  am very Thankful to the Author of this book “Let Us C”, my friend who referred it for me and who given opportunity  given to me share it with you.