Fonction de bibliothèque C - atoi ()

La description

La fonction de bibliothèque C int atoi(const char *str) convertit l'argument chaîne str à un entier (type int).

Déclaration

Voici la déclaration de la fonction atoi ().

int atoi(const char *str)

Paramètres

  • str - Ceci est la représentation sous forme de chaîne d'un nombre entier.

Valeur de retour

Cette fonction renvoie le nombre entier converti sous forme de valeur int. Si aucune conversion valide n'a pu être 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("String value = %s, Int value = %d\n", str, val);

   strcpy(str, "tutorialspoint.com");
   val = atoi(str);
   printf("String value = %s, Int value = %d\n", str, val);

   return(0);
}

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

String value = 98993489, Int value = 98993489
String value = tutorialspoint.com, Int value = 0