printf和scanf函数、putchar和getchar函数
输入输出操作都是由C标准函数库中的函数来实现的,要在程序文件开头用预处理指令#include把有关文件放在本程序中#include<stdio.h>
一、printf()输出详解:printf(格式控制,输出表列)
格式控制是用双引号括起来的字符串。简称格式字符串,例如:“%d”。输出列表是程序需要输出的一些数据,可以是常量、变量或表,例如a。
#include<stdio.h>int main(){printf("ddddd\n");}~[root@chenshuyi c]# ./printfddddd#include<stdio.h>int main(){int a = 100;printf("%d\tdddd\n",a); // \t是四个空格}~[root@chenshuyi c]# ./printf100dddd#include<stdio.h>int main(){int a = 100;printf("%d,%d,%d\n",a,a,a++);}[root@chenshuyi c]# ./printf101,101,100#include<stdio.h>int main(){int a = 100;printf("%d,%d,%d\n",a,a++,a++);}[root@chenshuyi c]# ./printf102,101,100#include<stdio.h>int main(){int a = 100;printf("%d,%d,%d\n",a,a,++a);}[root@chenshuyi c]# ./printf101,101,101
二、scanf()输入详细:scanf(%格式字符,变量地址)
#include<stdio.h>int main(){int a;scanf("%d",&a);}[root@chenshuyi c]# ./scanf2#include<stdio.h>int main(){int a, b;scanf("%d,%d",&a,&b);printf("%d,%d\n",a,b);}[root@chenshuyi c]# ./scanf2,32,3
三、getchar()和putchar(),输入和输出单个字符。
putchar()输出(凡是ASCLL码都可以放)。
#include<stdio.h>int main(){char chshyz = 97;putchar(chshyz);return 0;}~[root@chenshuyi c]# ./putchara
getchar()输入,是无参数的。
#include<stdio.h>int main(){char chshyz;chshyz = getchar();printf("%c\n",chshyz);return 0;}~[root@chenshuyi c]# ./getcharchshyzc
PS:统计输入的字符串大小写字母的个数(一个字母循环一次)
#include<stdio.h>int main(){char chshyz;int small = 0;int big = 0;int i = 0;for(chshyz = getchar(); chshyz!='\n';){if (chshyz >= 'a' && chshyz <= 'z') //比较的是ASCLL码small++;else if (chshyz >= 'A' && chshyz <= 'Z')big++;chshyz = getchar();i++;printf("第%d次循环\n", i);}printf("小写:%d个\t大写:%d个\n", small, big);return 0;}~[root@chenshuyi c]# ./getcharChshyz第1次循环第2次循环第3次循环第4次循环第5次循环第6次循环小写:5个 大写:1个