Category Archives: C/C++


How to Compile ‘n’ Execute/Run C/C++ program in Ubuntu 12.04

Now a Days, Ubuntu is one of the most popular OS among programmers (not only nerds but also the beginners who just entered into the programming world) but for a beginner Ubuntu user coming from Windows may feel some problem in programming with Ubuntu.

On Windows they use nice GUI based IDE, but in Ubuntu things are little different. In Ubuntu it’s better to use Terminal (specially for the beginners), instead of GUI based IDEs such as Eclipse, Netbeans etc for compiling programs. In fact, the command line approach is much easier and efficient, due to the powerful shell such as bash, zsh etc.

So, in this post – you will learn – how to compile and execute (run) C/C++ programs in Ubuntu 12.04 (Precise Pangolin)/12.10(Quantal Quetzal) or other Linux distributions such as Linux Mint 13 (Maya). You don’t need to install any extra applications or tools other than the compiler. The default text editor – gedit will work fine for source code editing (unless you have a preferred source editor such as Emacs, Vim, Nano, Kate or something else).

#1. Install the C/C++ Compiler

First make sure that you have GCC (GNU Compiler Collection, for C language) and G++ (for C++ language) installed on your system. If not, then install it by typing the following commands on Terminal –

For C

sudo apt-get install gcc

For C++

sudo apt-get install g++

#2. Write the Program (Source Code)

Open gedit and write the following lines of code (it’s a simple program that contains one print line and one comment, just for explanation purpose) –


void main()
	printf("Hello! Human!\n");
	/* Do something more if you want */

Then save the file as hello_human.c on your Home Directory (~). If you ae writing a c++ program, then give it the extension as .cpp (and of course you will also have to change the command accordingly during compilation)

#3. Compile it

The command structure is :

gcc source_file_name.c -o executable_file_name

if leave the -o option (name of the output file) then by default a.out will be created as the executable output file.

To execute the above example program, open a terminal and type –

cd ~
gcc hello_human.c -o hello_human

[In case of C++, just replace gcc with g++, rest of the things are same, e.g g++ source_file.cpp -o executable_file]

#4. Execute It

On Terminal, type (from the same directory where you have the executable file, in this case, it’s Home Directory(~) ) –

First make the script executable (sometimes, it may not be necessary)

chmod +x hello_human

Then run the program using the command –


Now you should get the output –

Hello! Human!
c programming in ubuntu 11.10

How to Compile and Run C/C++ program on Ubuntu 11.10

Ubuntu 11.10 (Oneiric Ocelot) is one of the most popular operating system for programming because there are lot of great open source applications, tools, compilers, debuggers, IDEs are available free of cost. Some of them are  – GCC – The greatest compiler for C language (from FSF (‘Free Software Foundation’ by Stallman); Linux Torwalds used GCC while developing Linux Kernel), Eclipse IDE (The most popular ‘Integrated Development Environment’ for Java programmers), Netbeans, KDevelop, Codelite  etc.

C/C++ language is a high level programming language (although the term high and low is used in relative sense e.g C is a high level language as compared to Assembly but if we compare it with java then C is a low level programming language; the term high or low basically describes the closeness with hardware). Most of the operating systems has been written in C language. This post has been written for beginners who just started learning C/C++ or the programmers who have migrated from Windows to Ubuntu (although the commands are almost same for all Linux based operating system).

Compiling and Executing C program in Ubuntu 11.10

1. Write and save the program

c programming in ubuntu 11.10

Open a simple text editor (e.g gedit), IDE (Eclipse) or command line code editor (Nano or Vim). I’ll be using gedit as it is very simple to use and it’s recommended for beginner programmers. Right Click on Desktop or any directory (Browse File using Nautilus) and select create new File – hello.c (.c extension is used to indicate that it’s a c program). Then write a simple program like this (and save the program press Ctrl+S)


void main()
	printf("Hello! This is my first C program with Ubuntu 11.10\n");
	/* Do something more if you want */

2. Compile the program

GCC (GNU Compiler Collection) is installed by default, in Ubuntu. To compile the program, open the terminal and move on to the target directory type the command – (where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program)

gcc hello.c -o hello1

If there is no syntax/semantic error in you program then the compiler will successfully generate an executable file, otherwise fix the problem in your code.

3. Execute the program

To execute the program, you need to run –


running a C program in Ubuntu 11.10

Compiling and Executing C++ program

The steps are almost same as above but you need to install g++ compiler, the file extension should be .cpp and in compilation phase replace gcc with g++. To install G++ compiler, execute the command –

sudo apt-get install g++

If you have any problems then share it through comments. One more thing, This video might help you in running your first C program in Ubuntu 11.10(I’ve recorded it in Gnome Shell interface) –

Programming with gcc

GCC-GNU Compiler Collection version 4.6.0 is out!

GCC(GNU Compiler Collection, version 4.6 was released a day ago) is a compiler available free under GPL by GNU Foundation. it is cross platform and is written using c/c++. Actually GCC has a major role in the growth of open source/Free Software world, in the past few years.

A majority of C/C++ programmers uses GCC, on Linux based operating systems as well as on Windows/Mac OS X. Since I uses Ubuntu 10.10 as my primary desktop; I know how important ,the gcc is, for programming Fun. GCC is mainly used as a compiler for C,C++,java,Fortran,Pascal,Ada,Objective-C.

A lot of extra features can be added with help of freely available plugins. GCC is one of the best compiler for c programmers.

Programming with gcc

New Features in GCC 4.6

  • Support for GO Programming Language has been added
  • New Optimizer Levels(-Ofast) has been introduced
  • Improvements in compiler time and Memory Usage
  • other platform specific improvements

GCC vs 4.6.0 has released(on March 25,2011). Go here for Complete details on the features of GCC.