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.
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.
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.
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 { }.
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.
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.
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 :)
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 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.
Previous lesson: Hello-world in C |
Next lesson: Data types in C |