Comment convertir un entier en char en C ?
Un caractère en C est déjà un nombre (le code ASCII du caractère), aucune conversion n'est nécessaire.
Si vous voulez convertir un chiffre en caractère correspondant, vous pouvez simplement ajouter '0' :
c = i +'0';
Le '0' ; est un caractère de la table ASCll.
Vous pouvez essayer la fonction de la bibliothèque atoi(). Les fonctions sscanf() et sprintf() peuvent également vous aider.
Voici un petit exemple pour montrer la conversion d'un entier en chaîne de caractères :
main()
{
int i = 247593;
char str[10];
sprintf(str, "%d", i);
// Now str contains the integer as characters
}
Voici un autre exemple
#include <stdio.h>
int main(void)
{
char text[] = "StringX";
int digit;
for (digit = 0; digit < 10; ++digit)
{
text[6] = digit + '0';
puts(text);
}
return 0;
}
/* my output
String0
String1
String2
String3
String4
String5
String6
String7
String8
String9
*/