Linux Mall - The Linux SuperStore!
Linux books, Linux CDs, Linux toys, you name it, they have it!

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

Newbie's Linux Manual
How Do I Create, Compile And Run a C Program?
] [ Download*] [ Previous] [ Next] C Language Tutorial-- An excellent C manual, available to read online and to download. Home] [ Contents] [ Download*] [ Previous] [ Next] Homepage| The Last 5 Days| The Daily Linux News| The Linux Bits| Newbie's Linux Manual
The Best Linux Sites| Linux Book Reviews| A Windows Vendetta?
Diary of a Linux Newbie| Diary of an Open Source Newbie
The Linux Forum| Just For Fun Amazon - The World's Biggest Bookstore!
4.7 million books, CDs, videos, and DVDs available to buy! Webmaster| Manual's Copyright Terms
[
* 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 
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:

When you want to run an executable that's not in your current directory, enter the relative or absolute path to the executable. It's only when the executable is in your current directory that you have to precede it's filename with ./ .

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

Link

>>

[
* In Linux enter: unzip nlm.zip
© MM Linuxdot.org |