LinuxMall.com Got Linux? CD Offer
Newbie's Linux Manual
How Do I Create, Compile And Run a C Program?
by Laurence Hunter
[ Home ] [ Contents ] [ Download*] [ Previous ] [ Next ]
* In Linux enter: unzip nlm.zip
A 5 Minute Course in C

For now it's not important that you understand all them cryptic C commands. All that matters is that minutes from now, you'll have learnt how to create a program in C, compile it and run it. Then you can act all smug in front of your mates. ;)

- 1 -

At the command line, pick a directory to save you program and enter:

pico firstprog.c
Note

All C source code files must have a .c file extension.

- 2 -

Enter the following program:


#include <stdio.h> int main() { int index; for (index = 0; index < 7; index = index + 1) printf ("Hello World!\n"); return 0; }

- 3 -

Press Ctrl+O to save the file and Ctrl+X to exit.

- 4 -

Enter:

gcc -o myprog firstprog.c

...to create an executable called myprog from your source code (firstprog.c).

Here's a detailed discussion of the line above:

  1. gcc (GNU C Compiler) is passed...
  2. ...-o which means give the executable the name that follows (i.e. myprog)...
  3. ...and the program to compile (referred to as the "source code") is firstprog.c.

- 5 -

To run the program, enter:

./myprog
Note

The ./ is used to combat trojans, programs used by crackers to circumvent a system's security by "sneaking through the back door".

If myprog was not in your current directory, then the relative (relative to current directory) or absolute path (the full path from the root (/) directory) is used.

Link

>> C Language Tutorial - An excellent C manual, available to read online and to download.

[ Home ] [ Contents ] [ Download*] [ Previous ] [ Next ]
* In Linux enter: unzip nlm.zip

W W W . L I N U X D O T . O R G

The Newbie's Linux Manual is reproduced on LinuxMall.com by permission. The Newbie's Linux Manual is written and maintained by Laurence Hunter. You can find much more of Laurence's work at his site: www.Linuxdot.org

© 1999 Linuxdot.org | Manual's Copyright Terms