scanf输入参数详解
1
|
scanf ( "%d %d" ,&a,&b);
|
1
2
3
4
5
6
7
8
9
|
#include <stdio.h>
int main( void )
{
int a,b,c;
printf ( "Give me the value of a,b,c seperated with whitespaces:\n" );
scanf ( "%d%d%d" ,&a,&b,&c);
printf ( "a=%d,b=%d,c=%d\n" ,a,b,c);
return 0;
}
|
格式指令说明
编辑
*
域宽
格式说明符
长度修饰符
空白字符和非空白字符
编辑
空白字符
非空白字符
1
|
scanf ( "%d%d" ,&r,&c);
|
1
|
scanf ( "%d%*c%d" ,&x,&y);
|
1
|
scanf ( "%20s" ,&address);
|
1
|
scanf ( "%c%c%c" ,&a,&b,&c);
|
1
|
scanf ( "%dt%d" ,&x,&y);
|
1
|
%[ABC]
|
1
|
%[A-Z]
|
注意问题
编辑
1
|
scanf ( "%c%c%c" ,&a,&b,&c);
|
1
|
d e f
|
1
|
scanf ( "%c %c %c" ,&a,&b,&c);
|
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>
int main( void )
{
char a,b;
printf ( "input character a,b\n" );
scanf ( "%c%c" ,&a,&b); /*注意两个%c之间没有任何符号*/
printf ( "%c%c\n" ,a,b);
return 0;
}
|
1
|
1
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h>
int main( void )
{
char a,b;
printf ( "input character a,b\n" );
scanf ( "%c %c" ,&a,&b); /*注意两个%c之间的空格*/
printf ( "\n%c%c\n" ,a,b);
return 0;
}
|
1
|
scanf ( "%d,%d,%d" ,&a,&b,&c);
|
1
|
5,6,7
|
1
|
scanf ( "a=%d,b=%d,c=%d" ,&a,&b,&c);
|
1
|
a=5,b=6,c=7
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h>
int main( void )
{
int a;
printf ( "input a number" );
scanf ( "%d" ,&a);
printf ( "%ld" ,a);
return 0;
}
|
问题一
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <stdio.h>
int main( void )
{
char str[80];
scanf ( "%s" ,str);
printf ( "%s" ,str);
return 0;
}
|
1
|
I love you!
|
1
|
I
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
#include<stdio.h>
#include<windows.h>
int main( void )
{
char str[80],str1[80],str2[80];
scanf ( "%s" ,str); /*此处输入:I love you!*/
printf ( "%s\n" ,str);
Sleep(5000); /*这里等待5秒,告诉你程序运行到什么地方*/
/**
*不是sleep(5)
*1,函数名是Sleep不是sleep。
*
2,Windows API中,unsigned Sleep(unsigned)应该是毫秒ms.
*/
scanf ( "%s" ,str1); /*这两句无需你再输入,是对stdin流再扫描*/
scanf ( "%s" ,str2); /*这两句无需你再输入,是对stdin流再扫描*/
printf ( "%s\n" ,str1);
printf ( "%s\n" ,str2);
return 0;
}
|
1
|
I love you!
|
1
2
3
|
I
love
you!
|
1
2
3
4
5
6
7
8
|
#include<stdio.h>
int main( void )
{
char str[50];
scanf ( "%[^\n]" ,str); /*scanf("%s",string);不能接收空格符*/
printf ( "%s\n" ,str);
return 0;
}
|
问题二
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include<stdio.h>
int main( void )
{
int a;
char c;
while (c!= 'N' )
{
scanf ( "%d" ,&a);
scanf ( "%c" ,&c);
printf ( "a=%dc=%c\n" ,a,c); /*printf("c=%d\n",c);*/
}
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include<stdio.h>
int main( void )
{
int a;
char c;
while (c!= 'N' )
{
scanf ( "%d" ,&a);
fflush (stdin);
scanf ( "%c" ,&c);
fflush (stdin);
printf ( "a=%dc=%c\n" ,a,c);
}
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>
int main( void )
{
int i;
char j;
for (i=0;i<10;++i)
scanf ( "%c" ,&j); /*这里%前没有空格*/
printf ( "%c" ,j); /*在输入十个字符之后*/
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>
int main( void )
{
int i;
char j;
for (i=0;i<10;++i)
scanf ( " %c" ,&j); /*注意这里%前有个空格*/
printf ( "%c" ,j); /*在输入十个字符之后,验证打印出来的字符是否是自己输入的最后一个字符(即输入的第十个字符)*/
return 0;
}
|
问题三
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>
int main( void )
{
int a=0,b=0,c=0,ret=0;
ret= scanf ( "%d%d%d" ,&a,&b,&c);
printf ( "第一次读入数量:%d\n" ,ret);
ret= scanf ( "%d%d%d" ,&a,&b,&c);
printf ( "第二次读入数量:%d\n" ,ret);
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>
int main( void )
{
int a=0,b=0,c=0,ret=0;
ret= scanf ( "%d%d%d" ,&a,&b,&c);
printf ( "第一次读入数量:%d\n" ,ret);
ret= scanf ( "%c%d%d" ,&a,&b,&c);
printf ( "第二次读入数量:%d\n" ,ret);
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
|
#include<stdio.h>
int main( void )
{
int a=0,b=0,c=0,ret=0;
ret= scanf ( "%d%d%d" ,&a,&b,&c);
fflush (stdin);
printf ( "第一次读入数量:%d\n" ,ret);
ret= scanf ( "%d%d%d" ,&a,&b,&c);
fflush (stdin);
printf ( "第二次读入数量:%d\n" ,ret);
return 0;
}
|
问题四
1
2
3
4
5
6
7
8
9
|
#include<stdio.h>
int main( void )
{
int a,b,c;
scanf ( "%d,%d" ,&a,&b);
c=a+b; /*计算a+b*/
printf ( "%d+%d=%d" ,a,b,c);
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
|
#include<stdio.h>
int main( void )
{
int a,b,c;
while ( scanf ( "%d%d" ,&a,&b)!=2)
fflush (stdin);
c=a+b;
printf ( "%d+%d=%d" ,a,b,c);
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/*此函数可以和scanf函数一起使用,但使用%c输入时要注意,即此函数只能用于缓冲区非空的情况*/
#include<stdio.h>
void flush()
{
char c;
while ((c= getchar ())!= '\n' &&c!=EOF);
}
intmain( void )
{
int a,b,c; /*计算a+b*/
while ( scanf ( "%d%d" ,&a,&b)!=2)
flush();
c=a+b;
printf ( "%d+%d=%d" ,a,b,c);
return 0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include<stdio.h>
int main( void )
{
inti,c;
while (1)
{
printf ( "Pleaseinputaninteger:" );
scanf ( "%d" ,&i);
if ( feof (stdin)|| ferror (stdin))
{
//如果用户输入文件结束标志(或文件已被读完),或者发生读写错误,则退出循环
//dosomething
break ;
}
//没有发生错误,清空输入流。通过while循环把输入流中的余留数据“吃”掉
while ((c= getchar ())!= '\n' &&c!=EOF);
//可直接将这句代码当成fflush(stdin)的替代,直接运行可清除输入缓存流
//使用scanf("%*[^\n]");也可以清空输入流,不过会残留\n字符。
printf ( "%d\n" ,i);
}
return 0;
}
|
scanf输入参数详解相关推荐
- 对于刷oj时因为scanf()出现wa而cin却AC的详解 【scanf() 和 cin 详解】
故事还得从昨天讲起,昨天做了一道题及其的诡异,用cin输入AC了.用scanf()却一直的报错或者陷入了 死循环.这让我很费解,我用了fflush(stdin)来排除,发现没有效果.后来我想起之前写过 ...
- scanf()函数的详解以及使用时需要注意的一些细节-C语言基础
这篇文章要探讨的是"scanf()函数的详解以及使用时需要注意的一些细节".涉及scanf()函数的应用和需要注意的问题.属于C语言基础篇(持续更新). scanf()(函数原型: ...
- ping ip 端口_学生会私房菜【20200305期】——Ping命令及其常用参数详解
一命令原理 Ping是ICMP的一个典型应用.Ping是检测网络连通的常用工具,同时也能收集其他相关信息.用户可以在Ping命令中指定不同参数,如ICMP报文长度.发送的ICMP报文个数.等待回复响应 ...
- Nginx内置变量以及日志格式变量参数详解
Nginx内置变量以及日志格式变量参数详解 $args #请求中的参数值 $query_string #同 $args $arg_NAME #GET请求中NAME的值 $is_args #如果请求中有 ...
- sklearn 随机分割数据_sklearn.ensemble.RandomForestClassifier 随机深林参数详解
随机森林是一种元估计量,它适合数据集各个子样本上的许多决策树分类器,并使用平均数来提高预测准确性和控制过度拟合.子样本大小由max_samples参数bootstrap=True (default)控 ...
- stylecloud.gen_stylecloud() 参数详解
参数详解: def gen_stylecloud(text=None,file_path=None, # 输入文本/CSV 的文件路径size=512, # stylecloud 的大小(长度和宽度) ...
- Lesson 8.1Lesson 8.2 决策树的核心思想与建模流程CART分类树的建模流程与sklearn评估器参数详解
Lesson 8.1 决策树的核心思想与建模流程 从本节课开始,我们将介绍经典机器学习领域中最重要的一类有监督学习算法--树模型(决策树). 可此前的聚类算法类似,树模型也同样不是一个模型,而是一类模 ...
- tf.nn.softmax参数详解以及作用
tf.nn.softmax参数详解以及作用 参考地址:https://zhuanlan.zhihu.com/p/93054123 tf.nn.softmax(logits,axis=None,name ...
- pythonsvc_sklearn-SVC实现与类参数详解
sklearn-SVC实现与类参数 它是基于libsvm实现的.随着样本数量的增加,拟合时间的复杂度要高于二次,这就使得当样板数量超过一万个时,很难扩展到数据集中. 在多类处理时,是按照1对1的方案进 ...
最新文章
- RDKit | 天然产物的相似度评分(NP-likeness)
- python turtle画椭圆-【python turtle如何画椭圆】
- mysql索引查2遍_mysql索引优化查询
- numpy报错:ValueError: numpy.ufunc size changed, may indicate binary incompatibility.
- 窗体的Alpha通道透明色支持
- pyculiarity解决报错ValueError: data must be a 2 column data.frame, with thefirst column...
- Swift——常量变量
- rda冗余分析步骤_文献综述,步骤与问题!
- 从0到1简易区块链开发手册V0.6-实现打印区块
- Python自动化整理文件,还增加了可视化GUI界面
- 自学python能找到工作吗-25岁从零开始学习python还能找到工作吗?
- 如何在VSCode上新建Flutter项目【两步搞定】
- 蛋白+小分子配体md(详细保姆教程)
- LR破解版录制手机脚本
- python父亲节礼物_盘点父亲节礼物,爸爸们的礼物也要用心挑选~
- Listen failure: Couldn‘t listen on 10.30.3.17:8000: [WinError 10049]
- Licheepi zero SPI Flash 系统编译
- camera sensor crosstalk 详细介绍
- 【Matlab笔记】测绘工程专业正算、反算、度分秒转弧度函数
- onlyoffice 安装、测试、打包、部署
热门文章
- 一组很有意思的图片——人生的意义
- C#使用ManagementClass获取计算器硬件信息
- Ubuntu下MongoDB的安装与使用(离线安装方式|非apt-get)
- Factorio异星工厂搭建服务器
- tmp是什么文件?tmp文件用什么打开?图文讲解
- `英语` 2022/8/20
- 刚刚!2022年QS世界大学排名发布:清华北大跻身前20!国内大学排名创纪录......
- 电信运营商网络运维方案
- GWAS曼哈顿图总结
- No.4-Heist-难度简单-HTB-walkthrough