User Tools

Site Tools


programming:c:hello_world

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
programming:c:hello_world [2013/08/05 15:19] – created zufdprogramming:c:hello_world [2014/12/11 16:22] (current) czuber
Line 1: Line 1:
 ====== Hello world ! ====== ====== Hello world ! ======
-===== Exercice 1 =====+===== Exercice 1 : printf =====
  
 +  * Open a terminal
 +  * Create your file by writing the following command
 +<code c>
 +vim ex1.c
 +</code>
 +  * Enter insertion mode by pressing Insert
 +  * Write the lines below
 <code c> <code c>
 #include <stdio.h> #include <stdio.h>
Line 9: Line 16:
  printf("Hello world !\n");  printf("Hello world !\n");
  
- return ;+ return ;
 } }
 </code> </code>
 +  * Then press Esc key
 +  * Write :wq
 +  * Compile your code with the following line
 +<code>
 +gcc -Wall ex1.c -o ex1
 +</code>
 +  * Then run your programm by typing "./ex1" in the terminal
  
 +
 +
 +===== Exercice 2 =====
 +
 +  * Open a terminal
 +  * Copy the content of the first exercice to the second by writing the following command
 +<code>
 +cp ex1.c ex2.c
 +</code>
 +  * Open ex2.c with vim
 +<code>
 +vim ex2.c
 +</code>
 +  * Edit your file so it looks like this :
 +<code c>
 +#include <stdio.h>
 +
 +int main(){
 +#ifdef DEBUG
 + printf("Hello world !\n");
 +#endif
 +
 + return 0 ;
 +}
 +</code>
 +  * Compile your code with gcc
 +<code>
 +gcc -Wall ex2.c -o ex2
 +</code>
 +  * 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
 +<code>
 +gcc -Wall -DDEBUG ex2.c -o ex2
 +</code>
 +  * Run it again
 +  * Hello world !
 +===== Exercice 3 : scanf =====
 +
 +  * Open a terminal
 +  * Create your file by writing the following command
 +<code c>
 +vim ex3.c
 +</code>
 +  * Enter insertion mode by pressing Insert
 +  * Write the lines below
 +<code 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;
 +}
 +</code>
 +  * Then press Esc key
 +  * Write :wq
 +  * Compile your code with the following line
 +<code>
 +gcc -Wall ex3.c -o ex3
 +</code>
 +  * Then run your programm by typing "./ex3" in the terminal
 +===== Exercice 4 : for =====
 +
 +  * Open a terminal
 +  * Create your file by writing the following command
 +<code c>
 +vim ex4.c
 +</code>
 +  * Enter insertion mode by pressing Insert
 +  * Write the lines below
 +<code 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;
 +}
 +</code>
 +  * Then press Esc key
 +  * Write :wq
 +  * Compile your code with the following line
 +<code>
 +gcc -Wall ex4.c -o ex4
 +</code>
 +  * Then run your programm by typing "./ex4" in the terminal
 +===== Exercice 5 : for in for =====
 +
 +  * Open a terminal
 +  * Create your file by writing the following command
 +<code c>
 +vim ex5.c
 +</code>
 +  * Enter insertion mode by pressing Insert
 +  * Write the lines below
 +<code 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;
 +}
 +</code>
 +  * Then press Esc key
 +  * Write :wq
 +  * Compile your code with the following line
 +<code>
 +gcc -Wall ex5.c -o ex5
 +</code>
 +  * Then run your programm by typing "./ex5" in the terminal
programming/c/hello_world.1375715943.txt.gz · Last modified: 2013/08/05 15:19 by zufd