1.输出字符和字符串
#include <iostream>
int main() {
putchar(80);
char ch = 'A';
putchar(ch);
puts("Hello World!\n");
char str[] = "Hello Clion";
puts(str);
return 0;
}
2.格式化输出
#include <stdio.h>
int main() {
printf("Number:%d,Unsigned Int:%u\n",12,23);//%u 无符号数字 不能写负数
printf("%o\n",8);//%o 八进制
printf("%x,%X,red:%X\n",15,15,0xFFFF0000);//0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10 十六进制
printf("%.2f\n",3.14);//%.2f 保留两位小数
printf("%c\n",'A');//字符
printf("%s\n","Hello World!");//字符串
printf("%03d",7);//%03至少三位有效数字 7---> 007 70---> 070 700---> 700 7000---> 7000
return 0;
}
3.输入字符
#include <iostream>
int main() {
puts("please input a char");
char inputChar = getchar();
printf("User input is %c\n", inputChar);
return 0;
}
4.格式化输入
#include <iostream>
int main() {
char dist;
printf("please input a char \n");
scanf("%c", &dist);
printf("User input char is %c\n", dist);
int a;
printf("please input a number\n");
scanf("%d", &a);
printf("User input number is %d\n", a);
char buf[100];
printf("Please input a string\n");
scanf("%s", buf);
printf("User input string is : %s\n", buf);
return 0;
}