如下的代码是关于C语言中char类型转换成int类型范例的代码,希望能对小伙伴们有所好处。
a[i] - ‘0’
参考程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[10];
int i, len;
while(scanf("%s", str) != EOF)
{
for(i = 0, len = strlen(str); i < len; i++)
{
printf("%d", str[i] - '0');
}
printf("n");
}
return 0;
}
int类型转化为char类型
a[i] + ‘0’
参考程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int number, i;
char str[10];
while(scanf("%d", &number) != EOF)
{
memset(str, 0, sizeof(str));
i = 0;
while(number)
{
str[i ++] = number % 10 + '0';
number /= 10;
}
puts(str);
}
return 0;
}
C语言中单引号和双引号的区别1、含义不同用单引号引起的一个字符实际上代表一个整数,整数值对应于该字符在编译器采用的字符集中的序列值。而一般我们的编译器采用的都是ASCII字符集。因此’s’的含义其实和十进制数115的含义是一致的。而用双引号引起的字符串,代表的是一个指向无名数组起始字符的指针。2、大小不同用单引号引起的一个字符大小就是一个字节。而用双引号引起的字符串大小是字符的总大小+1,因为用双引号引起的字符串会在字符串末尾添加一个二进制为0的字符’0’。