详解C语言中的char数据类型及其与int类型的转换

2020-01-06 13:41:21王冬梅

int类型转化为char类型

转换方法

 

 
  1. a[i] + '0' 

参考程序

 

 
  1. #include <stdio.h>   #include <stdlib.h>  
  2. #include <string.h>    
  3. int main()   {  
  4. int number, i;   char str[10];  
  5.   while(scanf("%d", &number) != EOF)  
  6. {   memset(str, 0, sizeof(str));  
  7.   i = 0;  
  8. while(number)   {  
  9. str[i ++] = number % 10 + '0';   number /= 10;  
  10. }   puts(str);  
  11. }    
  12. return 0;   }