字符类型
char是一种整数,也是一种特殊的类型——字符,因为:
用单引号表示的字符字面量’a’,‘2’
''也是一种字符
printf和scanf可以用%c来输入输出字符。
#include<stdio.h>
int main(){
char c;
char d;
c=1;
d='1';
if(c==d){
printf("相等\n");
}else{
printf("不相等\n");
}
printf("%d",c);
printf("%d",d);
return 0;
}
字符的输入和输出
#include<stdio.h>
int main(){
char c;
scanf("%c",&c);
printf("%d\n",c);
printf("%c\n",c);
return 0;
}
#include<stdio.h>
int main(){
int i;
char c;
scanf("%d",&i);
c=i;
printf("%d\n",c);
printf("%c\n",c);
return 0;
}
带不带有何不同?
#include<stdio.h>
int main(){
int i;
char c;
scanf("%d %c",&i,&c);
printf("i=%d,c=%d,c='%c'\n",i,c,c);
return 0;
}
scanf("%d %c",&i,&c);
printf("i=%d,c=%d,c='%c'\n",i,c,c);
如果带了空格,他会读入一个整数,然后空格几个都不要紧,然后再读入一个字符。如果没有空格,他就会把空格当作下一个字符读进去。