Loops, Functions, ModulesC
Loops, Functions, ModulesC
-------
There are : while(condition){}, do{}while(condition), for(variable; condition;
incrementation or decrementation){}
***Caution!! Don't forget to increment or decrement the loop's variable to get out
of the loop.
For "do{}while", incrementation or decrementation is done in do-brackets.
When the loop will contain just one line of code, we put that whithout brackets
e.g: if(age< 18 && age >1) printf("You cannot drink alcool)
Function
---------
To create a function we must : Give the return'type of the function, the
function'name and the type
and the name of his parameter : type name(type pameter's name){}
e.g:
int location(int x){x = 100; return x;)
***We have to declare a function before the main function. Then, the variable which
will be used by
the function we created has to be too declare before the main function. If we
create a function
after the main function, we must use a prototype before the main function:
The prototype is formed by : type of the return'function we created after main,
name of his(type'var)
e.g:
int a;
int main(void) {
a = add(a);
printf("%d", a);
printf("hello world);
return 0;
int add(int number)
{number = 0; return number;}
}
***A function can have no-parameter and no-return function
e.g: **void hello(void) printf("Hello, you're beautiful")
Modules
-----------
for creating a module, we need two principal things: a ".c" file and a ".h" file.
The ".c" file will contain all functions of the module; and the ".h" file(for Head
file)
will contain all prototypes of functions include in the ".c" file.
when all is done, to use the module, we must include the ".h" file in the place we
want to
use our module.
e.g
----
---------
player.c
---------
#include <stdio.h>
--------
main.c
--------
#include <stdio.h>
#include "player.h"
int main(void)
{
hello();
return 0;
}
***Caution: we must compile all files at the same time : gcc *.c -o executable
name;
module's files (".c" and ".h") can get different names, but, it's usefull to give
them the
same one; to include a library we created, we have to put #include "file.h" on the
top of our
main file.
At the start of our ".h" file, we can add a "#ifndef __a name__" and "#define __the
same name__".
At the end of our ".h" file, we have to add "#endif". That process will help to
protect our code
of infinite inclusion. For the code meaning...Translate by yourself.
Static var or function is use to conserve the variables value no matter the end of
the function
because, normally, at the end of a function all variables are destroyed, but, the
keyword static
helps to conserve them.
In the case of functions, static means this function cannot be used in an other
file than the one
which contains the function.