C Language Program

Source and Header files

A typical C language program contains two type of files – source and header files. We put the logic in the source files and more precisely in functions.

Headers (.h)

Advertise on this site. I promise you will like the rates :)

The header files have a .”h” file extension. They are helpers and normally don't contain any logic. In a well written program, the headers contain all declarations of functions and variables. We include a given header inside our source, when we want to use a function or variable, declared in that header.

In the hello-world example we use the header "stdio.h", using the line

    #include <stdio.h>

but more about this - in a minute. At this moment you don't need to know more about headers. Later in the tutorial, you will create your own header files, when you create a bit more complex programs.

Source (.c)

The source files for the C language have a .c file extension. They contain the source code that we write. Any C language program must contain at least one source file. In one of our source files we have to create a function with the name “main”. An exception to this rule is when our application will be compiled as a library and not as an executable application.

main.c source fileThe source file in our example C language program

The “main” function

Any computer program must have a starting point. Think of the programs that you use. For example, the first thing a web browser does is to open a window. Then it can display a blank page, your start page or something else, but it always starts from a fixed point – the new window.

In C, every program starts from a function, called “main”. You can see it in different prototypes, like

int main(), int main(void), void main(), int main(int argc, char **argv)

C is case sensitive, so if you name your function MAIN, the compiler will not recognize it as the starting point for the program.

Functions always have a body, which is located after the function prototype. The body is always enclosed in curly brackets { }.

Execution of a C language program

To explain how a C language program executes I will use the example from the last lesson. It contained only one source file. In my case that was “main.c”. It looks like this:

1
2
3
4
5
6
7
#include <stdio.h>

int main()
{
    printf("Hello world!\n");
    return 0;
}

This file will start the execution of our program, because it contains the main function. The code is executed line by line.

Preprocessor directive #include

The first line in main.c is

1
#include<stdio.h>

This command is a preprocessor directive. We place them in the top of the files and they always start with the symbol “#”. Preprocessor directives are instructions to the C preprocessor. These commands are executed before the compilation. The #include directive tells the preprocessor to copy the contents of a given header file in the current file(you will not see any code added to your source file).

Don't worry if this is a bit confusing, right now. We need this line to use the function printf which can output information on the screen.

After the preprocessor directives are done, our code compiles and it is ready to execute. The execution starts from the body of the main function. Remember, the body is enclosed in curly brackets.

Whitespace

Note that in my example the opening bracket is located on the next line below the function name. In your case it could be on the same line with the name of the function, this is OK too.

int main()
{
...
int main(){
...

C ignores almost all whitespace characters like space, tab or new line. Placing the opening bracket on the same or new row is a matter of coding style. It does not affect the way the code works.

In that respect, always keep your code well formated. Probably the most common mistake my students make is to neglect code formatting. Yes, early when your programs are very short this is not a problem. The problem is that now you build your coding habits and having good coding habits is good :)

Output with printf

Finally, we arrive at the actual line that does the only thing our program does:

        printf("Hello, world!\n");
    

This line calls the function printf and tells it to output the greeting “Hello, world!”. Note that the message must be put in quotes. The symbols “\n” are not a typo. This combination is the symbol for new line. This means that after our message, printf will move to the next line in the output console.

The return 0; statement

The line “return 0;” just ends the execution of the main function and with that it ends our program. It also notifies the OS that the program completed successfully. In C, a return code 0 means "success" and anything else means that the program did not complete in the way we intended to. A common way to notify of an error is to return 1.

Now you should have a basic understanding how a C language program works. There are many new terms that we don't explain right now, but this is OK. By the end of the tutorial you will understand them very well.

It is time to do your homework and continue to the next lesson.

Homework

  1. Answer the questions:
    a) What types of files we use in our C programs?
    b) In which files we place our logic?
    c) What is the purpose of a header file?
    d) What symbol precedes the preprocessor directives?
    e) What is the name of the function that is first started in any C language  program?
    f) Should you arrange the code of your programs, even if they are very short?
    g) Why we needed to use the #include<stdio.h> preprocessor directive?

  2. Change the hello-world example to welcome you, using your name. In my case it says “Hello, Miroslav!\n”.

  3. Use several times printf to output a matrix 4x5, filled with zeros. The output should look like this:
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0

Tutorial navigation

Previous lessonHello-world in C

Next lesson: Data types in C

   Search this site: