Find us on Google+ How To Compile A C Program in Ubuntu - Invatac - Technology Blog

Pages - Menu

Tuesday, 4 September 2012

How To Compile A C Program in Ubuntu


In this we can discuss how to compile and run a C Program in Ubuntu

Normally C Compiler is installed in Ubuntu by default. If you are not able to find the compiler we can start discussing from installing Compiler.


Step #1 : You have to install build-essential package. build-essential package will install C libraries to Ubuntu.




for that open terminal and type :


you can open terminal by "Ctrl + Alt + T " or find it from your Dashboard
 
sudo apt-get install build-essentials


Step #2 : Create a folder for storing your C Programs (Optional). But for better understanding, it is good to create a New Folder.
You can create Folder different ways, use anyway that is comfortable for you.

Open up terminal and type : "mkdir C" (This will create a Folder named 'C')

Then navigate to newly created folder by issuing following command 

cd c

Step #3 : Create the C source code with any of the text editors available in Ubuntu. You can use gedit or other text editor and save the source code in the folder that you created earlier.


You must save the file with extension .c


#include<stdio.h>
main()
{
         printf("\n My First C Program In Ubuntu \n");
         return 0;
}

Save the file as first.c


Step #4 : Compile Your first c program in Ubuntu

* in terminal

cc -c first.c
this will create an object file with default name "a.out".


but as a better programmer, it is better to create an executable with following code


* in terminal type:


cc -o first first.c

This will create an executable file named "first" in respected folder itself.

Step #5 : Run Your first C Program with the executable that you created


* in terminal type:

./first
Result : 

 My First C Program In Ubuntu

Hope you got what i was trying to convey.

 



Rahul Babu RAbout Me
I am a computer student looking for make my own name in the field of technology. Please add my to your circles.




4 comments:

  1. Compile and run the program in a single step:

    gcc first.c -o first && ./first

    ReplyDelete
    Replies
    1. Thanks for the Info. But it is not a good way to Run and Compile at one go ....

      Delete
    2. There is nothing wrong with the method that I have mentioned. If the program doesn't compile then the run portion after the && will not execute.

      Delete
    3. Thanks Bro For your info :)

      Delete