Fonctions de la bibliothèque C -atoi()

Bibliothèque standard C - <stdlib.h>

Description

Fonctions de la bibliothèque Cint atoi(const char *str)Passez le paramètrestrLa chaîne pointée est convertie en un entier (de type int).

Déclaration

Ci-dessous la déclaration de la fonction atoi().

int atoi(const char *str)

Paramètre

  • strLa chaîne à convertir en un entier.

Valeur de retour

Cette fonction renvoie l'entier long converti ; si aucune conversion valide n'est effectuée, elle renvoie zéro.

Exemple

L'exemple suivant montre l'utilisation de la fonction atoi().

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
   int val;
   char str[20];
   
   strcpy(str, "98993489");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   strcpy(str, "example.com");
   val = atoi(str);
   printf("字符串值 = %s, 整型值 = %d\n", str, val);

   return(0);
}

Compilons et exécutons le programme ci-dessus, ce qui produira le résultat suivant :

字符串值 = 98993489, 整型值 = 98993489
字符串值 = example.com, 整型值 = 0

Bibliothèque standard C - <stdlib.h>

autres extensions