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.
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<stdio.h>
main()
{
printf("Hello World\n");
printf("My First C Program\n");
}
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 followingmain()
{
printf("Hello World\n");
printf("My First C Program\n");
}
$ gcc firstprogram.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 hello firstprogram.c
Would create an executable by the name hello for your source code named firstprogram.c
Running the executable that you created is as simple as typing the following at the prompt.
$ ./hello
Or whatever you named your executable.
2 comments:
Thanx a lot for this tutorial. I am new to linux for me it was even hard to find text editor and terminal to run program.Some how I managed to find and tried this tutorial.It really worked.
Great work..!!!
:)
thanks a lot for this tutorial ....
Post a Comment