totn C Language

C Language: First Program

This C tutorial explains how to write your first application in the C language with instructions and code examples.

How to write your First Program in C

Let's start with a twist on the common "Hello World" application.

In your favorite editor, enter the following C source code and save the file as hello.c.

/* Include the standard IO library which includes the printf function */
#include <stdio.h>

/*
 * Every program requires a main function
 * This is the function the system will call when executing the program
 * In this example, we will leave out the usual arguments which are passed to main
 */
int main()
{
   /* Display our message */
   printf("Hello TechOnTheNet!\n");

   /* Return a non-error code to the system */
   return 0;
}

You will need a C compiler installed on your computer to compile the application. For the purposes of this example, we will assume that you are using a UNIX system with the GNU C compiler (gcc) installed.

Compilers can have very different options and interfaces so please consult the documentation for the version you are using.

To compile this application using gcc you would type:

gcc hello.c -o hello

Since this is a very basic example, we do not need to provide any additional libraries or information to gcc.

The result will be an executable file named hello that can be run by typing:

./hello

When run, the application will output:

Hello TechOnTheNet!

Congratulations, you have compiled your first C program!