Thursday, December 31, 2009

Geany - Nice IDE for C/C++ development

Geany is a fast and lightweight IDE for C/C++ programming in Ubuntu environment. It is open source (free) and anyone who has access to internet connection and Ubuntu installed on their system, can install it using following command on terminal:

sudo apt-get install geany

Alternatively, if you know and like to work with Synaptic package manager, then it is more intuitive to install Geany IDE using the SPM.

After installation, you can run Geany from Applications --> Programming --> Geany, click on Geany to run it you would see something like following screens on your desktop/laptop: (Click on any image given in this post to view it full size)




Key features of this nice and fast IDE are following:

1. It is very fast in compilation, buiding executables, and running code;
2. You can run terminal inside this IDE to switch to your current working directory and run your executable, to debug your code through gdb;
3. It does not have irritating GUI, where many unnecessary things are bombarded;
4. You can insert file headers, like date, time, General public license, source headers(stdio.h etc...) very quickly;
5. It supports many prominent languages like, C, C++, Java, Fortran, Caml, D, FreeBasic, Haskell, Haxe, Pascal, Assembler source files.


I would go through a bare basic example of C language to compile, build exe, running the exe.

Creating your first C source code, there are two ways you can do this:

(1.) create any folder anywhere in your system, name it whatever you wish, say “sample”, now make an empty file name it whatever you want to give to your C source file, for instance, say “sample.c”, give the extension .c this is necessary, double click on “sample.c”, it should open directly in Geany workspace, if you don't have any other IDE installed, but if you have, then it may not open in Geany by default, in this case, you would have to explicitly open it with Geany (right click on “sample.c” and open with Geany!). Now, you have your file opened in Geany workspace.

(2.) just open Geany IDE from Applications --> Programming --> Geany and click down arrow near New in the menu bar of Geany, you would see options like, C source file, C++ source file etc... click on C source file and you are done.

Now, from one of the above two steps, you have created your first C source file. You would like to see some action now, ok, if you are at any stage in C language (student, beginner, geek, professional, expert), you should be able to write any small piece of C code to see how Geany compiles, builds exe, and runs. I assume that you have or you will write any snippet within few minutes, now let's test it.
1. Compile: click F8 to compile(builds object file sample.o) and see how fast it compiles;
2. Build: click F9 to build executable(buids exe sample) and see how fast it does so.
3. Run: you can either run from terminal inside Geany or you can simply press F5 to run the executable you just build.
That's it!
Following are some screenshots giving you how nice it looks like programming with Geany and also demonstrates some of the points described above.



Any comments, suggestions and queries are most welcome.

Cheers!
Rajendra

Monday, December 28, 2009

Given n-th prime, find (n+1)-th prime!

Problem: Given n-th prime, how do you find (n+1)-th prime? Write a function
int GetNextPrime(int n);
Example: 
Input: 7 Output: 11
Input: 13 Output: 17
Hint: Search for Bertrand's Postulate!