当前位置:网站首页>C语言输入和输出(printf和scanf函数、putchar和getchar函数)
C语言输入和输出(printf和scanf函数、putchar和getchar函数)
2022-04-23 05:48:00 【Chshyz】
printf和scanf函数、putchar和getchar函数
输入输出操作都是由C标准函数库中的函数来实现的,要在程序文件开头用预处理指令#include把有关文件放在本程序中#include<stdio.h>
一、printf()输出详解:printf(格式控制,输出表列)
- 格式控制是用双引号括起来的字符串。简称格式字符串,例如:“%d”。
- 输出列表是程序需要输出的一些数据,可以是常量、变量或表,例如a。
#include<stdio.h>
int main(){
printf("ddddd\n");
}
~
[root@chenshuyi c]# ./printf
ddddd
#include<stdio.h>
int main(){
int a = 100;
printf("%d\tdddd\n",a); // \t是四个空格
}
~
[root@chenshuyi c]# ./printf
100 dddd
#include<stdio.h>
int main(){
int a = 100;
printf("%d,%d,%d\n",a,a,a++);
}
[root@chenshuyi c]# ./printf
101,101,100
#include<stdio.h>
int main(){
int a = 100;
printf("%d,%d,%d\n",a,a++,a++);
}
[root@chenshuyi c]# ./printf
102,101,100
#include<stdio.h>
int main(){
int a = 100;
printf("%d,%d,%d\n",a,a,++a);
}
[root@chenshuyi c]# ./printf
101,101,101
二、scanf()输入详细:scanf(%格式字符,变量地址)
#include<stdio.h>
int main(){
int a;
scanf("%d",&a);
}
[root@chenshuyi c]# ./scanf
2
#include<stdio.h>
int main(){
int a, b;
scanf("%d,%d",&a,&b);
printf("%d,%d\n",a,b);
}
[root@chenshuyi c]# ./scanf
2,3
2,3
三、getchar()和putchar(),输入和输出单个字符。
- putchar()输出(凡是ASCLL码都可以放)。
#include<stdio.h>
int main(){
char chshyz = 97;
putchar(chshyz);
return 0;
}
~
[root@chenshuyi c]# ./putchar
a
- getchar()输入,是无参数的。
#include<stdio.h>
int main(){
char chshyz;
chshyz = getchar();
printf("%c\n",chshyz);
return 0;
}
~
[root@chenshuyi c]# ./getchar
chshyz
c
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]# ./getchar
Chshyz
第1次循环
第2次循环
第3次循环
第4次循环
第5次循环
第6次循环
小写:5个 大写:1个
版权声明
本文为[Chshyz]所创,转载请带上原文链接,感谢
https://blog.csdn.net/HelloWorld_4396/article/details/124332758
边栏推荐
- 爬取彩票数据
- Cf6d lizards and fundamentals 2 problem solving
- Example of reentrant lock thread waiting to wake up
- Rust:如何 match 匹配 String 字符串?
- [leetcode 6] zigzag transformation
- Guaba and Computational Geometry
- Doomsday (simple computational geometry)
- A solution to replace not in in SQL
- POJ - 2955 brackets interval DP
- 队列解决约瑟夫问题
猜你喜欢

Mysql database foundation

Detailed arrangement of knowledge points of University probability theory and mathematical statistics

Robocode教程7——雷达锁定

Motor and drive (Qi Jinqing Edition)

Type conversion in C #

Solution to the trial of ycu Blue Bridge Cup programming competition in 2021

ArcGIS表转EXCEL超出上限转换失败

基于Sentinel+Nacos 对Feign Client 动态添加默认熔断规则

Addition, deletion, modification and query of MySQL table

Addition, deletion, query and modification of data
随机推荐
Type conversion in C #
批量导出Arcgis属性表
Collections multiple parameter sorting
Kalman filter and inertial integrated navigation
Storing inherited knowledge in cloud computing
Common shortcut keys of IDE
Solution to the trial of ycu Blue Bridge Cup programming competition in 2021
小区房价可视化
Robocode教程7——雷达锁定
[leetcode 19] delete the penultimate node of the linked list
基于QQwebAPI 查询昵称和头像的爬虫
gst-launch-1.0用法小记
Miscellaneous 1
The onnx model of yolov5 removes the transfer layer
[untitled] database - limit the number of returned rows
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
Rust 的 Box指针
SQL -- data definition
使用TransmittableThreadLocal实现参数跨线程传递
[leetcode 383] ransom letter