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

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

这也解释了上面这段代码能判断编译器默认char类型。

char型数字转换为int型

转换方法

 

  1. a[i] - '0' 

参考程序

 

 
  1. #include <stdio.h>   #include <stdlib.h>  
  2. #include <string.h>    
  3. int main()   {  
  4. char str[10];   int i, len;  
  5.   while(scanf("%s", str) != EOF)  
  6. {   for(i = 0, len = strlen(str); i < len; i++)  
  7. {   printf("%d", str[i] - '0');  
  8. }   printf("n");  
  9. }    
  10. return 0;   }