C Language

To share this page click on the buttons below;

The first C program

It is time for the first complete C program.

We suppose that the following code will be contained into a single C source file, we can name it as we prefer, let say first.c.

#include <stdio.h>

int sum(int a, int b);

int sum(int a, int b) {
    return a + b;
}

void main(void) {

    int c;

    printf("Hello World!\n");
    printf("This is my first complete C program!\n");

    c = sum(5,3);
    printf("The sum of 3 and 5 is: %d\n",c);
}

This program, once compiled and executed, will produce the following output:

Hello World!
This is my first complete C program!
The sum of 3 and 5 is: 8

There are several things to notice:

  • The main function is the starting point of each C program, every C program has one (and only one) main function. It turns out that the main function can return a value and have arguments, but for the moment we can neglect these details. In this case, in fact, you can see that the main function return void and takes void parameters. Remember that the word void is used by C to say "nothing" (no arguments and no return value).
  • The main function does not need to be declared, because it has to be present
  • We use the function sum that we design erlier. This function is first declared and then defined.
  • As for every other C function also the main one has a body enclosed between curly brackets.
  • Within the main body an integer variable c is declared (but not defined)
  • then variable c is finally defined as the result of the function sum.
  • To print something on the screen we use the function printf: I will talk about this function just here below along with the first line of the program #include <stdio.h>.

Print on screen

We want our program to print some messages on the screen, but printing is a really complex task, it involves a lot of details to take care of: fortunately we do not need to write our own printing function, we can rely on a standard C library.

In practice our program needs to use ready to use functions that are already implemented and stored in the standard C library. The standard C library is something that cames along with the compiler, it provides a lot of different functions ready to be used in your programs.

The C standard library is organized in different modules: each module implement a certain set of related function. Commonly used modules are the ones that implement input / output operation (like printing on the screen) or math operations or functions to deals with text.

How we instruct our program to use the C standard library printf function? By including the proper header file. That is what happens at the very first line of the program.

The line #include <stdio.h> is a pre-processor directive (one of the lines involved in the pre-processing phase of the compilation process).

The pre-processor directives start with #, the #include one (roughly speaking) takes the content of the header file indicated on the right of the directive (stdio.h in this case) and expands it in place of the preprocessor directive. This single line is exploded with the content of the whole header file.

The header stdio.h stands for standard (std) input / output (io) and in fact it contains the declaration a lot of useful functions to deal with input and output. The actual implementation of those functions is contained in the C standard library in binary form.

That means that the implementation of these functions is delivered through a compiled version of the standard input output module (which was certainly developed as regular .c source file). C in fact is a compiled language and you can deliver libraries in binary format. Other programs can use the functions you packed into libraries as far as you provide them with the correspondent header files that explain how (basically arguments and returning values) to use those functions.

Just a short recap:

  • we want our program to print a message on the screen
  • we know that there is a standard C library that implement such function
  • we include the correspondent header file so that our program is aware of the existence of the printing function and how it works
  • and finally we use it in our program.

If you are a little bit curious you may think that if you look for the file stdio.h and you open it, you will be finally able to see the declaration of the printf function. Well yes and not. It can be. The problem is that a header file might include other header files (they will be all exploded in place of our #include preprocessor directive) and so the actual declaration of that function might be located elsewhere.

We usually refer to the declaration of a function as the prototype of the function.

printf

printf is quite a special function: it can have a variable number of argument. This is an interesting topic, but a little bit too much advanced, for the moment. So here I will just explain how to use it.

The first argument of the printf function is the text of the message you want to print on the screen, this text has to be enclosed in double quote. So if you write printf("Hello World!"); when you execute the program you will see the text Hello World! on the screen.

If you use the printf function twice like that:

printf("Hello World!");
printf("My name is HAL!");

you will get on the screen the text: Hello World!My name is HAL! exactly as you see, without spaces between the end of the first message and the beginning of the second. If you want to print the message on two different lines you have to tell the printf to start a new line by using the special character \n (that is considered a single character).

So if you modify the code like the following:

printf("Hello World!\n");
printf("My name is HAL!");

you will get:

Hallo World!
My name is HAL!

But with printf you can also print something variable. That is what happens at the last line of the program printf("The sum of 3 and 5 is: %d\n",c);. Here we print the result of a sum, so we want to print a number we do not know. To do that you need to put in the text message a placeholder to inform the printf that you want to print some variable whose value obviously you do not know when you are writing the program. The placeholder in this case is the special character %d. This tells the printf that in that position an integer argument will be printed (there are different placeholders depending on the type of what you want to print). What you want to print is put as a second argument of the function after the text (c in our case). That is why printf is able to manage a variable number of variable values to be printed: for each placeholder in the message a correspondent additional argument is expected and the order and type of the placeholders must match the order of the arguments.

To share this page click on the buttons below;