printf与scanf函数的返回值

简介: printf与scanf函数的返回值

引言


 由一道题引发对这两个C语言中常见函数返回值的思考与探索


 请看题,思考输出的结果是什么?


#include<stdio.h>
int main()
{
  printf("%d", printf("%d", printf("%d", 43)));
  return 0;
}

 可能很多小伙伴第一次看到这题时懵了,“什么,printf函数还有返回值?”,包括我也不例外。但是C语言中不仅有规定其返回值,而且表明了它的特殊用途。


printf函数


 先来看看函数声明


int printf( const char *format [, argument]... );

 返回值为int,即为整型


 那么,它代表了什么呢?


Return Value


Each of these functions returns the number of characters printed, or a negative value if an error occurs.


 这是MSDN里给出的解释,中文意思是返回打印的字符数,如果发生错误则返回负值


 举个例子:


int ret = printf("%d", 2023);

 那么如果代码是这样,因为2023占据4个字符,返回值则为4


 此时,你是不是能够回答开头的问题啦?


#include<stdio.h>
int main()
{
  printf("%d", printf("%d", printf("%d", 43)));
  return 0;
}

  没错,输出是4321。来,请再跟我分析一遍,首先打印43,43占据2个字符,所以返回2。接下来,打印2,而2占据1个字符,所以返回1。最后,打印1。所以,屏幕上输出结果就是4321。


 此时,你是不是已经基本掌握它的用法啦?再来看看这道变式题:


#include<stdio.h>
int main()
{
  printf("%d", printf("%d ", printf("%d ", 43)));
  return 0;
}

 有没有发现什么变化,没错,在每次打印时后面加上空格,打印的结果还是4321吗?(请自行思考)


scanf函数


 有了printf函数,那么怎么能少了scanf函数这个好基友呢?那我们再探讨一下scanf函数的返回值与用法吧。


 先来看看函数声明

int scanf( const char *format [,argument]... );

 返回值同样为int ,那么它有代表了什么意思呢?


 在MSDN中查询得知


Return Value


scanf  return the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned.


A return value of 0 indicates that no fields were assigned.


The return value is EOF for an error or if the end-of-file character or the end-of-string character is encountered in the first attempt to read a character.


 是不是看得有点头晕,哈哈没事,简单翻译一下就是返回成功转换和分配的字段数,


 返回值为0表示没有分配字段。


 返回值为EOF表示发生错误,或者第一次读取到字符串或者文件末尾


举个例子:


int main()
{
  int ch = 0;
  int ret = scanf(&ch);
  printf("%d\n", ret);
  return 0;
}

  此时没有为ch分配字段,也就是占位符,所以返回值为0


int main()
{
  int ch1 = 0;
  int ch2 = 0;
  int ret = scanf("%d%d", &ch1, &ch2);
  printf("%d\n", ret);
  return 0;
}

 此时成功分配了2个字段,所以返回值就为2


 但是,要注意以下情况:


int main()
{
  int ch1 = 0;
  int ch2 = 0;
  int ret = scanf("%d%d", &ch1);
  printf("%d\n", ret);
  return 0;
}

 这种还有字段未分配的情况,是标准中未定义的,所以会报错



 那么,说了这么多,它有什么具体用途呢?


if (scanf("%d%d", &a, &b) == 2)
  {
  //语句
  }

 1.它可以用来判断是不是输入的参数都成功转换并分配字段,保证参数的有效性


while (scanf("%d%d", &a, &b) != EOF)
{
  //语句
}

 2.它可以用来多组输入,只要没有读取到文件末尾,就一直循环输入 (这个方式特别是在网站刷  题的时候,经常会用到)


总结


读完这篇文章,你是不是对于常用的printf和scanf有更深入的了解呢?各位小伙伴在学习的过程中,也要多去思考和举一反三,以及善用比如MSDN这样方便好用的工具,来提高自己的学习效率。最后,思考题的答案放在评论区哦~

相关文章
|
1月前
|
C语言
你真的学会了printf和scanf函数吗?
你真的学会了printf和scanf函数吗?
|
9月前
|
C++
VS中出现的printf,scanf等函数不安全而报错的问题的全面解决方法
VS中出现的printf,scanf等函数不安全而报错的问题的全面解决方法
|
3月前
printf函数的返回值
printf函数的返回值
13 0
|
4月前
c中scanf函数注意点
c中scanf函数注意点
30 0
|
4月前
|
缓存
scanf和printf函数
scanf和printf函数
57 0
|
5月前
|
编译器 C语言 C++
关于vs中scanf()函数报错问题的解决
关于vs中scanf()函数报错问题的解决
74 0
|
7月前
while(~scanf(“%d“,&a)&&~a)用法
while(~scanf(“%d“,&a)&&~a)用法
58 0
|
10月前
|
人工智能
scanf函数与getchar函数区别
scanf函数与getchar函数区别
111 0
|
10月前
|
编译器
printf()scanf()函数使用
printf()scanf()函数使用
41 0
|
IDE 编译器 开发工具
VS 调用 scanf 的正确姿势
VS 调用 scanf 的正确姿势
272 0
VS 调用 scanf 的正确姿势