Archive | C Programming RSS feed for this section

30 November 2010 Comments Off

Beginning C Programming in Linux

Don’t ask a lot to begin C programming in Linux. Just grab your tools and start small. This time, I just wanna show you how to start C programming in linux. You can use any Linux distro. However, if you are beginner, just follow me using Ubuntu Linux.

1. Install the required packages.
First of all, you have to make sure that you have C compiler to compile your code. In linux, we use “gcc – GNU project C and C++ compiler”.

In Debian, Ubuntu and it’s family distro, all the basic programming tools for C programming can be found in build-essential package. Use this command to install this package:

$ sudo apt-get install build-essential

2. Write your first program
Now, after installing the required package, we can start coding our first program. As usual, you may start with a “Hello World” program.

Create new project directory
Under your home directory, you may start creating your project directory to begin with. In this example, I’m calling it as myhello:

$ mkdir myhello

Then, get into our project directory:

$ cd myhello

Now, create our hello.c file. You may follow this example “hello world” code or be imaginative to modified it as you like. Experiments to see what is possible to modify in this code:

#include <stdio.h>

int main() {

printf("Hello My friend, Let's do C programming\n");

3. Compile your program.
To compile your first linux C program, launch your terminal and go to current directory where you put your hello.c code. Then, run this command:

$ gcc hello.c -o hello

After that, you will have an executable file called hello in this directory. Congratulations! you have created your first C program in linux. You can now, execute this program using this command:

$ ./hello

The output will be printed on your screen. You can add more stuff and experiments. Until next time, I hope you enjoy this. Happy coding…

Collected from:://

21 November 2010 Comments Off

How to Compile C++ in Ubuntu

Posted by Craciun Dan

Users who switch from Windows to Linux have often ask, how to compile C/C++ sources and what IDEs (Integrated Development Environment) Linux has to offer?

Most of them study C or C++ at school or home and are usually used from Windows with an IDE like Dev-C++ or Code::Blocks.

In this article I’ll give a few explanations on how to compile software for studying purposes on Linux (and particularly Ubuntu), what are the most common ways, what I consider to be the most effective method and which are the most popular applications to use for programming in those languages.

I’ll divert a little to say that Dev-C++, although a wonderful IDE on Windows, is no longer maintained, and even though a port used to be around for Linux, it was abandoned too (as far as I know). Instead, for those who would like a replacement which works and behaves the same way, I can warmly recommend Code::Blocks, which has an actively maintained port and it’s easy to compile and install. According to the details I could find on #ubuntu @ Freenode, Code::Blocks will also be included in the Intrepid Ibex (the next Ubuntu release) repositories, in universe.

Back to our topic. I think the simplest way to start with C/C++ in Ubuntu is to use first an editor like Nano and create a source file, then compile it using gcc (GNU Compiler Collection) in command line. But first, to install the GNU compiler and several other utilities for compiling sources, use:

sudo apt-get install build-essential

build-essential is a meta package – a package which only depends on other packages, so installing it will automatically install several tools like gcc, g++ and make.

Next, create your source file using a text editor of choice (I used Nano for this example):

nano main.c

Enter the content, e.g.:

int main ()
printf ("Hello, world!\n");
return 0;

Notice that I also included a newline after the close bracket, otherwise the compiler will issue a warning. Save it with CTRL+O, then exit Nano using CTRL+X. To compile your source, simply use:

gcc main.c -o myapp

The output, myapp, will automatically be executable, so to run it use:

ubuntu@ubuntu:~$ ./myapp
Hello, world!

This is the simplest way of creating and compiling C or C++ code.

Regarding more complex, powerful IDEs, you can try Vim, Emacs (which can be run both in CLI mode using emacs –no-window and in GUI mode) or even the user-friendly Nano. Nano can be configured by editing (or creating if it does not exist yet) the ~/.nanorc file, where ~ is your home directory. The global configuration file is located in /etc/nanorc. Also, you can read this tutorial on how to enable syntax highlighting in Nano.

Among the good editors which use a graphical interface are Kate, Gedit, Geany, KDevelop, Anjuta, Code::Blocks or Eclipse. These are not all though, but I recommend trying those first and see which one fits. I’ll briefly review some of them below, so you can have a general idea about each of them.

Its name means KDE Advanced Text Editor, but Kate is definitely not only a text editor. It supports highlighting in many languages, indentation, spell-checker, block selection mode, and it’s highly configurable. Kate comes by default in Kubuntu or can be installed using sudo apt-get install kate.

sudo apt-get install kate

This is the default text editor in GNOME. It can be used as a simple IDE too. It comes installed by default in Ubuntu.

sudo apt-get install gedit

Yet another editor written in GTK. It’s pretty light and includes the most common features an IDE should have, so it’s a good alternative to Gedit.

sudo apt-get install geany

This is the KDE advanced IDE, offering the tools and advanced features of a full IDE. I recommend starting with a text editor rather than using this one for studying purposes. However, if you especially want to develop KDE applications, KDevelop is the way to go.

sudo apt-get install kdevelop

This is the powerful port of Code::Blocks for Windows, using the wxWidgets interface. In my opinion it’s very fit for studying C/C++ on Linux. Although not included in Hardy Heron, Code::Blocks will be included in the Intrepid Ibex repositories.

Update: Ubuntu 9.04 comes with Code::Blocks included in the repositories, so you can install it using the usual sudo apt-get install codeblocks command. Ubuntu 8.10 and 8.04 users can follow the instructions below:

Notice: It is also a good idea to install xterm (a terminal application just like GNOME Terminal or Konsole), since Code::Blocks uses it to show the output of your programs.

sudo apt-get install xterm

1. Install the dependencies and compiler tools

sudo apt-get install build-essential
sudo apt-get install libwxgtk2.8-dev wx-common libgtk2.0-dev zip

2. Download the source code
Get the source from the official website, here, next uncompress it using:

tar -xjf codeblocks-8.02-src.tar.bz2

3. Compile it
Change the working directory to codeblocks-8.02-src and issue as usual:

sudo make install

Finally, run ldconfig as root:

sudo ldconfig

This should do it. You can run Code::Blocks by typing codeblocks in a terminal or pressing ALT+F2 and writing codeblocks in the run dialogue that appears.

Written in GTK, Anjuta is a powerful development environment for C and C++, which also allows you to create GNOME applications.

sudo apt-get install anjuta

Addition: NetBeans
NetBeans is an advanced IDE written in Java from Sun Microsystems, and can be used for developing C/C++ code too.

sudo apt-get install netbeans-ide

Addition: Eclipse CDT
The package eclipse-cdt provides the Eclipse IDE with C/C++ development plugins. I found it slower than the others IDEs mentioned here, especially the interface.

sudo apt-get install eclipse-cdt

Addition: CodeLite
CodeLite is an open-source IDE similar with Code::Blocks. To install it, download the latest Ubuntu build (DEB package) from the official website, make sure the current working directory is the one where you saved it, and type the following command (replacing the filename with the latest name):

sudo dpkg -i codelite_1.0.2785-ubuntu0_i386.deb


Tags: , ,
14 November 2010 Comments Off

Recursion or Re”curse”?


“Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C++, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to “repeat the process”. This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the “process” to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the “build wall” function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks.

void recurse()
recurse(); //Function calls itself

int main()
recurse(); //Sets off the recursion

This program will not continue forever, however. The computer keeps function calls on a stack and once too many are called without ending, the program will crash. Why not write a program to see how many times the function is called before the program terminates?


using namespace std;

void recurse ( int count ) // Each call gets its own count
cout<< count <<"\n";
// It is not necessary to increment count since each function's
//  variables are separate (so each count will be initialized one greater)
recurse ( count + 1 );

int main()
recurse ( 1 ); //First function call, so it starts at one

Recursively find the factorial of the smaller numbers first, i.e., it takes a number, finds the factorial of the previous number, and multiplies the number times that factorial…have fun. :-)

Use of recursion in an algorithm has both advantages and disadvantages. The main advantage is usually simplicity. The main disadvantage is often that the algorithm may require large amounts of memory if the depth of the recursion is very large.

Read more on Recursion:

26 October 2010 Comments Off

90+ Beginners Lab Assignments in C Langauge

  1. A Calculator in C Using Graphics & Mouse Operations
  2. A chat program in C
  3. A program to implement Heap Sort
  4. All types of Linked List Operations
  5. Analog and Digital clock
  6. Begginers of system programming
  7. Bomber Fighter Plane simulation
  8. Bubble Sorting Algorithm
  9. Calendar of Thousands of Years
  10. Calendar Program
  11. Calender Program in C
  12. Calender
  13. Class with constructor ( for bank account )
  14. Convert decimail nos to roman equivalent upto 10,000
  15. Converting Roman letter to number
  16. convertion of number to letters
  17. CPU Scheduling algorithm implementation
  18. Decimal to Binary, Octal and HEX converter
  19. encryption and decryption of files
  20. Finding LCM and GCD
  21. Frequency Based Histogram
  22. Graphical Calculator Design
  23. Guessing Game in C
  25. Invoke function without main in C Language
  26. Kite flying code in C
  27. Merge sort
  28. No guessing
  29. N-Queen’s Problem
  30. Factorial Function
  31. Prg. to convert upper case to lower case or lower case to upper case
  32. Prg. to correct rudimentary syntax errors
  33. Prg. to count no. of characters,no. of blanks,no. of words & no.
  34. Prg. to sort any no. of numeral i-p in ascending or descending order.
  35. Printint a double pyramid
  36. Program for Binary, Octal, Hexadecimal Conversions
  37. Program for computing Area, Volume and Perimeter of Rrectangle using
  38. Program for finding the prime numbers
  39. Program for finding the sum of digits of a five digit number
  40. Program for Prime Number Generation
  41. Program for rotating circles using maths.(sin,cos,Phase defference,e.t.c)
  42. Program Implementing the rot13 algorithm
  43. Program of Falling Characters
  44. Program to calculate frequency of vowels in a string
  45. Program to Calculate the Pascal triangle
  46. Program to calculate the sum of series


4 July 2010 Comments Off

C- is alphanumeric

#include <ctype.h>
#include <stdio.h>

int main(void)
char ch;

printf(". to exit:");
for(;;) {
ch = getc(stdin);
if(ch == '.')
printf("%c is alphanumeric\n", ch);

return 0;

Tags: ,