w w w . T H E B I T S . c o . u k


How Do I Create, Compile And Run a C Program?

Home Contents Previous Next

For now, it's not important that you understand all those cryptic C commands. All that matters is that minutes from now, you will 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 -

Save and exit Pico.

- 4 -

Enter:

gcc -o myprog firstprog.c

...to create an executable 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 'source code') is 'firstprog.c'.

- 5 -

To run this example program, enter:

./myprog

The './' means run a file located in the current directory. You could quite easily have entered something like:

/home/Laurence/myprog

...If '/home/Laurence' happened to be the directory 'myprog' was in.


Link

C Language Tutorial


By Laurence Hunter laurence@thebits.co.uk

Home Contents Previous Next

Download Latest Manual

www.THEBITS.co.uk - updated daily...