Example 48: Write a program, use the getchar function to read two characters into c1 and c2, and then use the putchar function and printf function to output the two characters.
Problem-solving ideas: think about three questions
Should the variables c1 and c2 be defined as character or integer?
What should I do if I want to output the ASCII codes of the c1 and c2 values?
Do integer variables and character variables replace each other under any circumstances?
Source code demo:
#include<stdio.h>//头文件
int main()//主函数
{
char character_1,character_2;//定义字符
printf("请输入两个字符;\n");//提示语句
character_1=getchar();//键盘输入字符character_1
character_2=getchar();//键盘输入字符character_2
printf("用putchar语句输出结果为:");//提示语句
putchar(character_1);//输出字符character_1
putchar(character_2);//输出字符character_2
printf("\n");
printf("--------------\n");//分隔符号
printf("用printf语句输出结果为:\n");//提示语句
printf("%c\n",character_1);//输出字符character_1
printf("%c\n",character_2);//输出字符character_2
return 0;//主函数返回值为0
}
The compilation and running results are as follows:
请输入两个字符;
xy
用putchar语句输出结果为:xy
--------------
用printf语句输出结果为:
x
y
--------------------------------
Process exited after 5.082 seconds with return value 0
请按任意键继续. . .
More cases can go to the public account: C language entry to mastery