Hello world !
Exercice 1 : printf
vim ex1.c
#include <stdio.h>
int main(){
printf("Hello world !\n");
return 0 ;
}
gcc -Wall ex1.c -o ex1
Exercice 2
cp ex1.c ex2.c
vim ex2.c
#include <stdio.h>
int main(){
#ifdef DEBUG
printf("Hello world !\n");
#endif
return 0 ;
}
gcc -Wall ex2.c -o ex2
Run it with “./ex2”
Nothing should happen, it's normal because you didn't tell the compiler to use preprocessor parts. Which are defined by the #ifdef #endif quotes
now recompile your program with the following line
gcc -Wall -DDEBUG ex2.c -o ex2
Run it again
Hello world !
Exercice 3 : scanf
vim ex3.c
#include<stdio.h>
int main(int argc, char** argv)
{
char nom[14];
printf("Salut\nComment t'appelles-tu ?\n");
scanf("%s", nom);
printf("Salut %s\n", nom);
return 0;
}
gcc -Wall ex3.c -o ex3
Exercice 4 : for
vim ex4.c
#include<stdio.h>
int main(int argc, char** argv)
{
int nbrMax;
int i = 1;
printf("Salut\nEntre un nombre\n");
scanf("%d", &nbrMax);
printf("Affichage de nombres de 1 à %d\n", nbrMax);
for(i=1;i<=nbrMax;i++)
{
printf("%d\n", i);
}
return 0;
}
gcc -Wall ex4.c -o ex4
Exercice 5 : for in for
vim ex5.c
#include<stdio.h>
int main(int argc, char** argv)
{
int i = 1;
int j = 1;
int nbrMax = 10;
for(i=1;i<=nbrMax;i++)
{
for(j=1;j<=nbrMax;j++)
{
printf("%d\t", i*j);
}
printf("\n");
}
return 0;
}
gcc -Wall ex5.c -o ex5