Big Tuto: Program with the C language
Chapter 3: Our first program!
Tutorial presented by: The Ludovyc and Jérémie F. Bellanger (Jay)
The Ludovyc
Publication: March 5, 2014
Last revision: March 9, 2025
In this chapter, we will finally create our first program! ![]()
Well, don't expect anything exceptional, but it will still be your first C program, and you won't be able to help but think back on it with a little tear in your eye, in 15 years!
No, I'm kidding! ![]()
I- Forward, all!
Code::Blocks / Visual Studio users: Here we go, so create a new project in your IDE (C for Code::Blocks and C++ for VS), open the main.c file and delete what's in there (Code::Blocks) or create a main.cpp file (VS), and you're ready to type some code.![]()
II- The main()
Here is the code for our first program:
int main()
{
}
There you go, see you soon for the next chapter!! ![]()
What, it's already over!?! But it's a scam!!
I didn't pay, but I still want a refund!! ![]()
Oops, sorry I couldn't help but make that joke! ![]()
Okay, more seriously, you might think that what we just coded is not much, but in fact it is the basis! Of course, if we compile and execute the program, nothing will happen at all... The console will just open and stay black...
But let me give you some explanations! ![]()
The main() is a function and I would even say it is THE function. Without it, there is no program in C (or in most languages for that matter...). It is the one and only function that is called when your program starts. It is therefore mandatory that a program contains a main() function to be able to start, and it will also be unique because, in C, two functions cannot have the same name!
The parentheses following the function name indicate the opening and closing of the parameters declaration, which is how we recognize a function: monster() will be a function for example while monster will be a variable (or a structure - see the Big SDL Tutorial
). The braces { and } indicate the opening and closing of the function body.
This form is however the minimal form of our main(). Indeed, we can add other instructions to it, which may prove useful later, as you create more and more complicated programs:
int main()
{
return 0;
}
You've probably already noticed the int that precedes the name of our function. This actually denotes an integer.
But then, why write "whole number" in front of the name of our function?! ![]()
In fact, it is mandatory and it indicates the type of variable that the program or function will return at the end of its execution. In our case, main() will return an integer (we will see the other types of variables later - but know that a function that will not return anything will be noted void
).
Yes, but what about return 0; in all this? ![]()
Well, it is not mandatory, but if we put it, it means that the program will return 0 if it ends correctly (without error). This can be useful to debug it later. In the case of a function, it allows to manage the rest of the program according to the number returned. If we take a function int DetectCollisions() for example, whose purpose would be to detect collisions between the player and monsters, if this function returns 0, everything is fine, there are no collisions, on the other hand, if it returns 1, the player is hit and dies: Game Over!
Okay, this is a bit of a radical example, but it's so you understand. ![]()
You will also notice the semicolon ( ; ) at the end of return 0; -> it is very important in computing because it indicates the end of an instruction (a bit like the period in a sentence). At first, you will probably forget it from time to time and your code will not compile. So look there first. ![]()
Your eagle eye will also have noticed that the return instruction is set back compared to the rest of the code. In this case, we are talking about indentation. It is not mandatory in C, but allows for better readability of the code. This indicates that return 0; is in the main() function. In other programming (or scripting) languages, it is mandatory to use indentation, such as in Python, for example.
The main() function is very special, so there is another more complete version, which takes two input arguments! To use some libraries like SDL 2, for example, you will have to adopt it. It is also the most standard form of main(), so it is preferable, even if you do not use these two input variables. But we will come back to that later! ![]()
int main(int argc, char *argv[])
{
return 0;
}
Note that the formatting of the code above (most often the default in IDEs) is only indicative and its only purpose is to air out the code so that it is readable. We could just as well write:
int main(int argc, char *argv[]){
return 0;
}
or even:
int main(int argc, char *argv[]){ return 0; }
It would work exactly the same way.![]()

III- Hello world!
It is customary during a tutorial or a course on a programming language that our first program is a "Hello World!"
). The goal of this minimal program is to show that we are able to compile and run a minimal program, the goal of which will be to write a message, indicating that everything works well. So let's not break the rule, and write our real first program. Here it is:
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("Hello world!");
return 0;
}
Now have fun changing the message (without removing the quotes
), to make your program display what you want.

Come on, let's go for some explanations!![]()
#include <stdio.h> allows you to add the standard C library, named stdio.h (for standard input/output), to your program. We add this library to have access to the "printf" function because we didn't code it: it is directly part of the standard C libraries. We will talk about adding libraries later, but just remember that if we don't add it, you won't be able to use the "printf" instruction...
You will notice that we do not put a semicolon (;) after the #include instructions.
The "printf" function takes a string as parameters, which is equivalent to the message that we want to display. To do this, this string must be in parentheses (which are used to indicate its type
). This string is then placed between the parentheses following the name of the instruction: we say that we fill the declaration body with the parameters whose order and type was predefined when the function was created. For now, this may seem like Chinese to you, but you will see that in a very short time, you will create your own functions and do all this very simply
.
And here is the end of our third chapter! I hope you enjoyed it and that you have already learned a lot! The next chapters will allow us to see how to create and use variables, then instructions like if or for before we start creating our own functions. ![]()
See you soon ! ![]()

English
Français