How to Write and Run C/C++ Programs in Ubuntu (Linux)



Hello Friends, as we all know that turbo c++ is very old compiler so recently i have started doing programming in ubuntu. Linux is becoming very popular among the programmers. In my opinion you should also start doing programming in linux. I thought that i should share the process to write and run c/c++ programs on ubuntu 

Also Read: Explain Basic Structure of C Programs? 
Also Read: Online Compilers: The Top 5 Websites to Write, Compile and Run Programs Online
 

1. First of all open terminal window, for this go to Applications> Accessories> Terminal, as shown in below image.

How to write and run c/c++ programs in Ubuntu
(click on the image to enlarge)

2. To run c/c++ program you need to install the essential packages. For this enter the command given below in terminal window.
sudo apt-get install build-essential

Now it will ask for the admin password. When you enter the password the intallation process will start.

Note: For intalling packages you must be connected to internet. It will take few minutes to complete, depending upon your internet speed.

3. Now its time to write and run the program. Below i have explained simple steps for both c and c++.

For C Program

1. Enter the command given below in terminal window to open text editor.
gedit prog.c

Here prog.c is the name of the program. After entering above code it will ask for password.

2. Now write your program, for your convinience i have given an sample program below, you can copy and paste it in gedit text editor.

#include<stdio.h>

int main()
{
printf("\nThis is C Programming in Ubuntu\n\n");
 return 0;
}

3. Now save and close the editor.
4. To compile the program enter the command given below in terminal window.
gcc prog.c -o prog

5. If there will be no error in the program then nothing will be shown. And if error occurs, it will be shown. In case you get an error you have to open the text editor again by repeating step 1 and remove it and again save and close the editor.

6. Enter the command given below to run the program.
./prog

7. Now you will see the output in the terminal window as shown in below image.

How to write and run c/c++ programs in Ubuntu
(click on the image to enlarge)

For C++ Program

1. Enter the command given below in terminal window to open text editor.
gedit progcpp.cpp

2. Now write your program, i have given an example below.


#include<iostream>
using namespace std;

int main()
{
cout<<"\nThis is C++ Programming in Ubuntu\n\n";
 return 0;
}

3. Now save and close the editor.


4. To compile the program enter the command given below in terminal window.
g++ progcpp.cpp -o progcpp

5. Enter the command given below to run the program.
./progcpp

As i have just started using ubuntu so it may be possible that i have missed some points in this tutorial, feel free to mention it in the comment section.

Subscribe To Get Tutorials Directly In You Inbox!

12 comments

enhancement is better than c compiler

Reply

enhancement is better than c compiler

Reply

Dude this tutorial sucks! What the fuck are you teaching?
(1) Your Ubuntu is version 8 or lesser. There's version 13.04(Raring, DVD)and 12.04(Precise Pangolin, CD) out there, they're free!
(2) Really? "#include"! What is this? 1998?
(3) Why not set -Wall flags and see what errors you'll encounter with those absurd codes?
(4) "using namespace std" are for beginners. I may be developing apps in Qt and alias stdout with a QTextStream cout, what happens then? Always write that this way -> std::cout

Reply

Thanks for informing me, i will surely take care of this thing in future.

Reply

There is no need to bash anyone here, the main aim of this tutorial was to demonstrate how one would go about compiling c/c++ code in ubuntu linux not how to code like a pro....and ubuntu being outdated or not the principles are all the same

Reply

do u know how to run cpp program in red hat 6.0

Reply

That's ok, Neeraj! I wondered that you used to program embedded systems, like me. So, no matter if the notation is the 90 or more of them, if your current code is eficente and have an adequate performance. The point here, in my opinios is: Let free to use Linux. Thank you for the tutorial.

Regards
Eduardo Luna

Reply

This method will definitely work in all linux versions. I have myself tried it in red hat. There may be slight changes.

Reply

Your welcome Eduardo, i am happy that you have liked the tutorial. Thank you so much for your nice comment.

Reply

What do you mean by "(2) Really? "#include"! What is this? 1998?" How else could you include any dependencies into that program?

Reply

thanx, but can i use third party ide like code.Blocks....On linux

Reply

Yes you can use, codeblocks is also available for linux.

Reply

Post a Comment

Have any Doubt? Ask Here!