Vous n'êtes pas identifié(e).
L'icône rouge permet de télécharger chaque page du wiki visitée au format PDF et la grise au format ODT →
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente Prochaine révision Les deux révisions suivantes | ||
utilisateurs:hypathie:tutos:memo-c [19/08/2014 16:21] Hypathie [Fonctions] |
utilisateurs:hypathie:tutos:memo-c [19/08/2014 20:38] Hypathie [fonction bis] |
||
---|---|---|---|
Ligne 144: | Ligne 144: | ||
valeur age: 23 valeur taille: 175 | valeur age: 23 valeur taille: 175 | ||
</code> | </code> | ||
+ | |||
====char : caractère, chaîne de caractères ==== | ====char : caractère, chaîne de caractères ==== | ||
Ligne 1243: | Ligne 1244: | ||
</code> | </code> | ||
- | ====Bibliothèque string.h==== | ||
- | ===fonction strcpy === | ||
- | |||
- | Elle permet d'utiliser la fonction **''strcpy''**. | ||
- | Cette fonction "strcpy" permet la manipulation des chaînes de caractères. | ||
- | |||
- | strcpy(nom-de-la-variable-declaree,"nlle-valeur"); | ||
- | |||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <string.h> | ||
- | int main(void){ | ||
- | char nom[5]="toto"; | ||
- | strcpy(nom,"tata"); | ||
- | printf("%s\n",nom); | ||
- | } | ||
- | </code> | ||
- | <code> | ||
- | tata | ||
- | </code> | ||
- | |||
- | => Voir le fichier "string.h" pour voir quelle autre fonction elle contient. | ||
- | |||
- | ===fonction strcat=== | ||
- | |||
- | Elle permet de rajouter à la suite.\\ | ||
- | "cat", comme "concaténer" ! | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <string.h> | ||
- | int main(void){ | ||
- | char nom[15]="bonjour"; | ||
- | strcat(nom," toto"); | ||
- | printf("%s\n",nom); | ||
- | } | ||
- | </code> | ||
- | <code> | ||
- | bonjour toto | ||
- | </code> | ||
- | |||
- | ===for dans chaîne de caractères=== | ||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <string.h> | ||
- | int main(void){ | ||
- | char nom[25]="Hello"; | ||
- | strcat(nom," Toto, are you ok ?"); | ||
- | int i; | ||
- | for(i=0;i!=25;i++){ | ||
- | printf("nom[%d]=%c\n",i,nom[i]); | ||
- | } | ||
- | printf("\n"); | ||
- | |||
- | } | ||
- | </code> | ||
- | |||
- | <code> | ||
- | nom[0]=H | ||
- | nom[1]=e | ||
- | nom[2]=l | ||
- | nom[3]=l | ||
- | nom[4]=o | ||
- | nom[5]= | ||
- | nom[6]=T | ||
- | nom[7]=o | ||
- | nom[8]=t | ||
- | nom[9]=o | ||
- | nom[10]=, | ||
- | nom[11]= | ||
- | nom[12]=a | ||
- | nom[13]=r | ||
- | nom[14]=e | ||
- | nom[15]= | ||
- | nom[16]=y | ||
- | nom[17]=o | ||
- | nom[18]=u | ||
- | nom[19]= | ||
- | nom[20]=o | ||
- | nom[21]=k | ||
- | nom[22]= | ||
- | nom[23]=? | ||
- | nom[24]= | ||
- | </code> | ||
- | > L'espace réservé peut-être plus large que les éléments. | ||
- | |||
- | ===Fonction strcmp=== | ||
- | |||
- | Elle permet de comparer. | ||
- | |||
- | <code c> | ||
- | #include <stdio.h> | ||
- | #include <string.h> | ||
- | int main(void){ | ||
- | char nom[10]="Hello b"; | ||
- | printf("%d\n",strcmp(nom,"Hello")); | ||
- | } | ||
- | </code> | ||
- | 32 | ||
- | <code> | ||
- | |||
- | </code> | ||
=====Les pointeurs ===== | =====Les pointeurs ===== | ||
====Définitions==== | ====Définitions==== | ||
Ligne 1556: | Ligne 1454: | ||
=====fonction bis ===== | =====fonction bis ===== | ||
+ | ====bibliothèque stdio.h ==== | ||
+ | ===fonction int et commande sizeof === | ||
+ | Un entier à une taille en octet. | ||
+ | <code c> | ||
+ | #include<stdio.h> | ||
+ | int main(void){ | ||
+ | printf("%d\n",sizeof(int)); | ||
+ | } | ||
+ | </code> | ||
+ | <code> | ||
+ | 4 | ||
+ | </code> | ||
+ | |||
+ | <note tip> | ||
+ | **Cela permet de comprendre l'utilisation de [[http://www.table-ascii.com/|la table ASCII]].**\\ | ||
+ | |||
+ | On pourrait faire : | ||
+ | <code c> | ||
+ | #include<stdio.h> | ||
+ | int main(void){ | ||
+ | printf("%d\n",sizeof(char)); | ||
+ | } | ||
+ | </code> | ||
+ | <code> | ||
+ | 1 | ||
+ | </code> | ||
+ | |||
+ | >1 octet = 8 bit | ||
+ | >8 bits permettent de coder 255 caractères différents. | ||
+ | >char peut donc "contenir la table ASCII qui va pour le code décimal des caractères alphanumériques (+des caractères spéciaux) qui va de 0 à 255. | ||
+ | |||
+ | <code c> | ||
+ | #include<stdio.h> | ||
+ | int main(void){ | ||
+ | int lettre=97; | ||
+ | printf("%c\n",lettre); | ||
+ | } | ||
+ | </code> | ||
+ | <code> | ||
+ | a | ||
+ | </code> | ||
+ | |||
+ | </note> | ||
+ | |||
===Pourquoi la fonction "main" renvoie-t-elle à un "int" ? === | ===Pourquoi la fonction "main" renvoie-t-elle à un "int" ? === | ||
Surtout qu'elle peut contenir des "char" ! | Surtout qu'elle peut contenir des "char" ! | ||
Ligne 1603: | Ligne 1545: | ||
>**C'est au système d'exploitation que le "int" renvoie des informations**. | >**C'est au système d'exploitation que le "int" renvoie des informations**. | ||
- | ===ce servir du "int" dans la fonction "main"=== | + | ===Se servir du "int" dans la fonction "main"=== |
<code c> | <code c> | ||
#include <stdio.h> | #include <stdio.h> | ||
Ligne 1629: | Ligne 1571: | ||
</code> | </code> | ||
> 0 est le code de retour. | > 0 est le code de retour. | ||
+ | |||
+ | |||
+ | ====Bibliothèque string.h==== | ||
+ | ===fonction strcpy === | ||
+ | |||
+ | Elle permet d'utiliser la fonction **''strcpy''**. | ||
+ | Cette fonction "strcpy" permet la manipulation des chaînes de caractères. | ||
+ | |||
+ | strcpy(nom-de-la-variable-declaree,"nlle-valeur"); | ||
+ | |||
+ | |||
+ | <code c> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | int main(void){ | ||
+ | char nom[5]="toto"; | ||
+ | strcpy(nom,"tata"); | ||
+ | printf("%s\n",nom); | ||
+ | } | ||
+ | </code> | ||
+ | <code> | ||
+ | tata | ||
+ | </code> | ||
+ | |||
+ | => Voir le fichier "string.h" pour voir quelle autre fonction elle contient. | ||
+ | |||
+ | ===fonction strcat=== | ||
+ | |||
+ | Elle permet de rajouter à la suite.\\ | ||
+ | "cat", comme "concaténer" ! | ||
+ | |||
+ | <code c> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | int main(void){ | ||
+ | char nom[15]="bonjour"; | ||
+ | strcat(nom," toto"); | ||
+ | printf("%s\n",nom); | ||
+ | } | ||
+ | </code> | ||
+ | <code> | ||
+ | bonjour toto | ||
+ | </code> | ||
+ | |||
+ | ===for dans chaîne de caractères=== | ||
+ | <code c> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | int main(void){ | ||
+ | char nom[25]="Hello"; | ||
+ | strcat(nom," Toto, are you ok ?"); | ||
+ | int i; | ||
+ | for(i=0;i!=25;i++){ | ||
+ | printf("nom[%d]=%c\n",i,nom[i]); | ||
+ | } | ||
+ | printf("\n"); | ||
+ | |||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | nom[0]=H | ||
+ | nom[1]=e | ||
+ | nom[2]=l | ||
+ | nom[3]=l | ||
+ | nom[4]=o | ||
+ | nom[5]= | ||
+ | nom[6]=T | ||
+ | nom[7]=o | ||
+ | nom[8]=t | ||
+ | nom[9]=o | ||
+ | nom[10]=, | ||
+ | nom[11]= | ||
+ | nom[12]=a | ||
+ | nom[13]=r | ||
+ | nom[14]=e | ||
+ | nom[15]= | ||
+ | nom[16]=y | ||
+ | nom[17]=o | ||
+ | nom[18]=u | ||
+ | nom[19]= | ||
+ | nom[20]=o | ||
+ | nom[21]=k | ||
+ | nom[22]= | ||
+ | nom[23]=? | ||
+ | nom[24]= | ||
+ | </code> | ||
+ | > L'espace réservé peut-être plus large que les éléments. | ||
+ | |||
+ | ===Fonction strcmp=== | ||
+ | |||
+ | Elle permet de comparer. | ||
+ | |||
+ | <code c> | ||
+ | #include <stdio.h> | ||
+ | #include <string.h> | ||
+ | int main(void){ | ||
+ | char nom[10]="Hello b"; | ||
+ | printf("%d\n",strcmp(nom,"Hello")); | ||
+ | } | ||
+ | </code> | ||
+ | |||
+ | <code> | ||
+ | 32 | ||
+ | </code> |