字符常量
#include <stdio.h> int main() { char c1,c2; c1='a'; c2='b'; printf("%c %c\n", c1, c2); return 0; }
字符当数字用?!
#include <stdio.h> int main() { char c1,c2; int i = 100; c1 = 'a'; i = i - c1; printf("c1: %c\n", c1); printf("c1: %d, i: %d\n", c1, i); c2 = c1 + 4; printf("c2: %c\n", c2); return 0; }
字符进行数值运算——ASCII是本质
#include <stdio.h> int main() { int i,j; i='A'; j='B'; printf("%d %d\n", i, j); printf("i+j=%d\n", i+j); char c1, c2; c1 = 97; c2 = 98; printf("%c %c\n", c1, c2); printf("%c %d\n", c1-32, c2-32); return 0; }
获得缓存区中的输入数据(运行时,分别输入abc<回车>和a<回车>查看结果)
#include <stdio.h> int main() { char c1,c2; c1=getchar(); c2=getchar(); printf("%c %c\n", c1, c2); printf("%d %d\n", c1, c2); return 0; }
转义字符用法示例
#include <stdio.h> int main() { printf("%.4f%.4f%.4f\n", 1.01, 2.2334, 3.4); printf("col1\tcol2\tcol3\n"); printf("%.4f\t%.4f\t%.4f\n",1.01, 2.2334, 3.4); printf("This\nis\na\n\t\ttest\n\nShe said, \"How are you?\"\n"); return 0; }
趣味转义字符
#include <stdio.h> int main() { printf("笑脸:\001\n"); printf("红黑桃:\003 \006\n"); printf("听个响:\a\n"); printf("换几行:a\012 b\n c\x0A d\n"); printf("看字母:AB\n"); printf("再看字母:\101\102\n"); printf("再看字母:\101\b\102\n"); printf("还看字母:\101\r\102\n"); return 0; }