scanf()和printf()不能在单个语句中工作吗? [英] Does scanf() and printf() not work in a single statement?

查看:70
本文介绍了scanf()和printf()不能在单个语句中工作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

非常感谢.
我不是C语言中的优秀程序员.我对此表示怀疑,这可能是理论上和概念上的问题.
以下代码始终给出错误的输出,输出始终为1?

Hello,

Very much Thanks in advance.
I am not a very good programmer in C. I have a doubt, it may be theoretical and conceptual problem.
Following code always gives wrong output, output is always 1?

#include <stdio.h>

void main()
{
  int a = 100; // even tried initialize with 200 still output will be 1
  printf("%d", scanf("%d", &a));
}



用户输入:10
输出:1

用户输入:99
输出:1

注意:我正在使用DevC ++环境.



User-input: 10
Output: 1

User-input: 99
Output: 1

Note: I am using DevC++ environment.

推荐答案

是的,但是您必须知道要打印的printf函数.
yes you can, but you must have to know what printf function will print.
//copied from cplusplus.com
On success, the function returns the number of items successfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read, EOF is returned.



要了解有关scanf的更多信息,请按照链接



to learn more about scanf follow the link


我可以理解您的问题犯了一些小错误.

U认为从用户读取的scanf返回值是错误的.

scanef返回成功读取的项目数.在您的情况下,一个(1)就是"a",用户给定的输入存储在一个not return中.

printf(%d",scanf(%d%d",& a,& b));

它会打印2,依此类推.

有关更多参考,请阅读以下内容: scanf参考 [
I can Understand ur problem have done a little mistake.

U think that scanf return value which it read from user but that is wrong.

scanef return number of items successfully read. in your case it is one(1) that is ''a'', The in put given by user is stored in a not return.

printf("%d", scanf("%d %d", &a,&b));

it will print 2 and so on.

for more referance read this: scanf Referance[^]


这篇关于scanf()和printf()不能在单个语句中工作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆