scroll post

Blogger Tips and TricksLatest Tips And TricksBlogger Tricks

Linux Basics: How to Compile & Execute C Programs

It's Time to Learn Linux Friends you know what, after April 8, 2014, technical assistance for Windows XP will no longer be available, including automatic updates that help protect your PC.
Microsoft will also stop providing Microsoft Security Essentials for download on Windows XP on this date. If you continue to use Windows XP after support ends, your computer will still work but it might become more vulnerable to security risks and viruses.

Government authorities are aware of the risk of continuing with Windows Xp and many of the 45 Indian banks using XP have shown little intent to switch completely to Windows 7 or 8.1. "State Bank of India has around 1.35 lacs PCs still running Windows XP and they are looking to upgrade these.

I heard that even the Indian government is also planning to move on to Linux and started training their workers to work on Linux as it is open source and free to use Operating System.

This article is for those guys who used to write programs under Windows.. and now have entered the Linux territory and want to learn Basics of Linux in programming. You have probably heard a lot about Linux and how you can do some real good programming under Linux. But right now you cant even get the simplest of Hello World programs to compile.

Here's how you do it -

Procedure :

You can type you C program using any of the editors that are available under Linux such as vi or emacs or any other editor. If you're familiar with notepad and you are started using Kali Linux just type "notepad" in the Terminal and write your program and save it as "helloworld.c"

Helloworld Program:

#include<stdio.h>
int main()
{
printf("\nHello World");
return 0;
}

Once you have written and saved your C program using any editor return to the prompt. An ls command should display your C program. It should have the .c extension. Now at the prompt type the following

$ gcc -o helloworld helloworld.c

If your file is named helloworld.c then type '-o helloworld' as the parameter to gcc. This is basically your suggested name for the executable file that gcc would create. In case you typed something like the following

$ gcc helloworld.c

You would be having a a.out in the same directory as the source C file. This is the default name of the executable that gcc creates. This would create problems when you compile many programs in one directory. So you override this with the -o option followed by the name of the executable

$ gcc -o hi hiworld.c

Would create an executable by the name hi for your source code named hiworld.c

Running the executable that you created is as simple as typing the following at the prompt.

$ ./helloworld
OR
$ ./hi

Or whatever you named your executable.


Happy Programming with Linux.............