First a little about C and its derivative C++.

It is considered to be the most commonly used programming language. If you want a job in programmming, you will need to know this language. But what makes C the language that everyone wants to use. Well, its been around for quite some time. The original C since the 70s, and C++ since the early 80s, which is like eons in computer time. C is the most versatile high level language. C permits just about anything, allowing programmers to write straight to the metal. Its code runs very fast. This tutorial will cover C and C++ (C with classes).

c programming tutorial

Table of Contents:

This tutorial is meant to be a brief introduction to C and C++, it is by no means exhaustive. If you need more information try looking at our Other Resources. Learning a programming language is a lot like learning a language that people speak like German or French. The best way to learn a 'human' language is to start speaking, listening, and repeating, and then leaving the grammar for later. The same can be applied to C, you need to start writing programs as quickly as possible. So, this tutorial will provided a lot of well commented sample programs that you can run and tinker with.

If you've ever read another tutorial or a book on a programming language, then you've probably seen a program like our first one. The hello world program. It is a perfect starting place for our tutorial. So, type the following program into your favorite editor. If you are using Windows then try notepad or DOS Editor, if using a Linux variant use their Editor, VI or emacs. Without further adew here we go :

Your First Program


#include <stdio.h>
 
main()
{
    printf("Hello World\n");
    return 0;
}

Example 1

Example 1 - C hello world program
A very simple c program printing a string on screen

/* hello world program */
#include <stdio.h>

void main()
{
    // print to screen
    printf("\nHello World\n");
}

Example 2

Example 2 - C hello world program
In this example void means that main returns nothing when the program ends

#include <stdio.h>

int main()
{
    printf( "Hello World.\n" );
    getchar();
    return 0;
}

Example 3

Example 3 - C hello world program
Also prints a string on screen with getchar() more about that later

Save this code into a file, and call the file hello.c, then compile it by typing at a command prompt:

gcc hello.c

This creates an executable file a.out

which is then executed simply by typing its name.
The result is that the characters Hello World are printed out,
preceded by a new line.