Back to index
<< Prev Next >>
Explaining our first C program
I am going to explain the program
foo.c we used on the first part going line by line.
Code: Select all
#include <stdio.h>
int main()
{
printf("Hello wololo.net/talk!\n");
return 0;
}
I'll try to keep it simple, so most of the concepts seen here will be explained with more detail later on.
As we've seen on part II, this is a pre-processor directive. It indicates that the file
stdio.h should be "included" here, which literally means copy-pasting that file here.
stdio.h is what's called a "header" file (.h). Header files are used in C to store prototypes, declarations, type definitions and data, but by convention they do not contain any actually executable code.
This is a C function declaration. This means we're going to detail a function named
main with no arguments (there's nothing between the parenthesis) that returns an integer (
int). A function is a short piece of code that usually does a very specific task.
main() function is a special function in C. It marks the point-of-entry, which means it's where the execution of our program will start. It's mandatory (your program MUST have a
main() function)
This marks the beginning of the function code.
Code: Select all
printf("Hello wololo.net/talk!\n");
printf() is also a function. It's declared on
stdio.h (that's why we include that file).
printf() prints a character string to the standard output. A character string is represented between double quotes (") in C. It accepts formatting, which are special characters used to format the presentation. For more information check the manual page for
printf (man 3 printf)
A very important thing on this line is the trailing ";" character. This indicates to the C compiler the end of a C expression. This is the way the C compiler distinguishes between expressions, so do not forget it!
This marks the end of the function and instructs it to return a value of 0. The type of the value returned by the
return statement must match the one declared on the function header. This effectively ends our program, returning this value to the operating system. On POSIX environments 0 means the program ended with no problem.
This marks the end of
main() function declaration.
See you on the next part
<< Prev Next >>
Advertising