Spread Firefox Affiliate Button

How To Write, Compile and Execute C Programs under Linux

Most Linux and Unix programs are written in C. When you download source for a project, it will often be C or C++ source code. You don't necessarily need to know a too much thing about C or anything else to compile the source if you aren't "changing it".

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. My favourite is vi editor.


Source Code:
Write a Hello World C Program: Create a file call "firstprogram.c" in vi and type the following content into this file and save it.



#include
#include
void main()
{
printf("Hello World\n");
printf("My First C Program\n");
}



Once you have written and saved your C program, return to the prompt. The “ls” command should display your C program. It should have the .c extension. Now at the prompt type the following


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


$ gcc -o hello firstprogram.c
This would create an executable by the name hello for your source code named firstprogram.c
Running the executable that you created is very simple. Just type the following at the prompt.

$ ./hello  Or whatever you named your executable.
Posted on 9/21/2009 01:32:00 AM by ket@n and filed under , | 0 Comments »

0 comments:

Post a Comment