Lessons
Variables :
int main()
{
int number; //Declare the variable number which is an integer (int)
number = 42; //Assign the value 42 to my variable number
numer = 3 + 3; //Assign the value 6 to my variable number
char letter; //Declare the variable letter which is a character (char)
letter = 'A'; //Assign the value A to my variable letter
return (0);
}
Functions :
The main function in C is the first function called in your program.
A function is composed in this order by a return value, the name and the parameters.
void my_putchar(char c) //The function is called my_putchar, returns nothing and takes a char in parameter
{
//c == 'A'
}
int main() //The function is called main, returns an integer and takes no parameters
{
my_putchar('A'); //Call the function my_putchar with the char 'A' in parameter
return (0); //The function return the integer 0
}
void my_putchar(char c) //The function is called my_putchar, returns nothing and takes a char in parameter
{
//c == 'A'
}
int main() //The function is called main, returns an integer and takes no parameters
{
my_putchar('A'); //Call the function my_putchar with the char 'A' in parameter
return (0); //The function return the integer 0
}
Conditions :
int main()
{
int a = 0, b = 12;
if (a == 0)
{
//If the variable a is equal to 0 the code goes here
}
else if (b != 12)
{
//Else if the variable b is different to 12 it goes here
}
else
{
//Else it goes here
}
return (0);
}
Loops :
int main()
{
for(int i = 0; i <= 5; i++) //The for loop
{
//Do something
}
//After the loop i == 5
char c = 'A'; //char c = 65 is the same (ASCII table)
while (c <= 'D') // The while loop
{
c++;
}
//After the loop c == 'D' or c == 68 (ASCII table)
return (0);
}
int main()
{
for(int i = 0; i <= 5; i++) //The for loop
{
//Do something
}
//After the loop i == 5
char c = 'A'; //char c = 65 is the same (ASCII table)
while (c <= 'D') // The while loop
{
c++;
}
//After the loop c == 'D' or c == 68 (ASCII table)
return (0);
}
Using my_putchar :
int main()
{
char print = 'a';
my_putchar(print); //Displays a
my_putchar('a'); //Displays a
my_putchar(97); //Displays a (ASCII table, 'a' is the same as 97)
my_putchar('7'); //Displays 7
print = print + 1;
my_putchar(print); //Displays b
return (0);
}
int main()
{
char print = 'a';
my_putchar(print); //Displays a
my_putchar('a'); //Displays a
my_putchar(97); //Displays a (ASCII table, 'a' is the same as 97)
my_putchar('7'); //Displays 7
print = print + 1;
my_putchar(print); //Displays b
return (0);
}
ASCII Table :
