Just this once, I will show you how the program should look, and explain the different parts of it.
#include <stdio.h>
int main(void)
{
int a = 23;
if (a > 18)
{
printf("You can drive\n");
}
return 0;
}
Now lets take each line one by one:
#include <stdio.h>
This isn't a C statement, it's a so-called preprocessor directive. It is an instruction to find and copy-paste the contents of the system header file stdio.h
into the code.
This specific header file is needed for the compiler to know about the printf
function used later.
int main(void)
This is a function definition (implementation). It defined a function called main
, which returns an int
value and takes no arguments. All C statements that are not declarations or definitions must be inside a function.
Execution of your program begins with a call to the main
function.
{
This begins the block of code that implements the main
function.
int a = 23;
This defines a variable named a
, and initialize it with the value 23
. Normal statements in C must be terminated with the semicolon ;
.
if (a > 18)
This is a conditional statement. If the condition inside the parentheses is true then the next statement will be executed.
The condition a > 18
fetches the value of the variable a
, and checks if it's larger than the value 18
. Since we just initialized a
to have the value 23
, the condition will be true.
{
This starts a new block of statements. It is the block of code that will be executed if the condition is true.
printf("You can drive\n");
Call the printf
function to print a string. The \n
character in the string is the newline, and means a new line will be printed at the end of the string. Also note that this statement is also terminated by a ;
.
}
This closes the block of code for the if
statement. Note that block-statements should not be terminated by a semicolon, the closing }
also acts as a terminator.
return 0;
Because we defined the main
function to return an int
value, we need to actually return a value. That is what the return
statement is doing. It will return immediately from the function, and in this case return the value 0
.
It's normal for a program to return 0
if everything is okay. If there's an error, it's common to return a small positive integer value.
}
This closes the block of code for the main
function, and marks the end of the program execution.
A good course, book or tutorial should have already told you all of this. If your tutorials haven't, then don't use them.
Unfortunately it's so easy to put up videos on Youtube, that there are millions of bad tutorials, and very hard to find something good. So my general advice is ti skip Youtube, and instead invest in some beginners books. Here's a list of mixed books.
Buying books is expensive, but in the long run that cost will be a good investment if you're really serious about learning C and programming.