1 1、接受用户从键盘上输入的两个字符,然后输出他们
#include <stdio.h> int main(int argc, const char * argv[]) { //定义两个变量储存字符 char ch1,ch2; //打印一句话引导用户输入 printf("please enter two characters, separate with return key:\n"); //将用户输入的值存入变量中 scanf("%c",&ch1); rewind (stdin); scanf("%c",&ch2); //将用户输入的字符打印出来 printf("the characters you have entered are %c and %c\n",ch1,ch2); return 0; }
2、接受用户从键盘上输入的两个双精度浮点数,然后输出他们
#include <stdio.h> int main(int argc, const char * argv[]) { //定义两个变量储存数 double num1,num2; //打印一句话引导用户输入 printf("please enter two numbers:\n"); //将用户输入的值存入变量中 scanf("%lf%lf",&num1,&num2); //将用户输入的数打印出来 printf("the number you have entered are %lf and %lf\n",num1,num2); return 0; }
3、接受用户从键盘上输入的两个单精度浮点数,然后输出他们(保留两位小数)
#include <stdio.h> int main(int argc, const char * argv[]) { //定义两个变量储存数 float num1,num2; //打印一句话引导用户输入 printf("please enter two numbers:\n"); //将用户输入的值存入变量中 scanf("%f%f",&num1,&num2); //将用户输入的数打印出来 printf("the number you have entered are %.2f and %.2f\n",num1,num2); return 0; }
4、用户从键盘上输入两个整数,然后输出他们和
#include <stdio.h>
int main(int argc, const char * argv[]) { //定义两个变量储存数 int num1,num2; //打印一句话引导用户输入 printf("please enter two numbers:\n"); //将用户输入的值存入变量中 scanf("%d%d",&num1,&num2); //将用户输入的数的和打印出来 printf("the sum of %d and %d is %d.\n",num1,num2,num1+num2); return 0; }
|