How to Run C Program in Linux Environment



Note : This tutorial is for those who used to write programs in Windows Environment and want to write programs in Linux Environment.

Steps :

1.Write Program

  • You can type C program using any of the editors that are available under Linux.
  • VI and EMACS are two famous editors in Linux.
  • I have Written Sample Code in Fedora 7. [Application -> Accessories -> Editor]

2. Save Program

  • Save your C program with .C Extension .
  • Open Prompt or Terminal .
  • An ls command will display your C program.
  • Now at the prompt type the following

$ gcc -o mypgm mypgm.c

  • Suppose your file is named mypgm.c then type ‘-o mypgm ‘ as the parameter to gcc.
  • Mypgm is name for the executable file that gcc would create.

This Process can be Split into Two Processes -

Step 2.1 :

$ gcc mypgm.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 ]

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

$ ./mypgm