不同的scanf格式之间的差异 [英] Difference between different scanf formats

查看:218
本文介绍了不同的scanf格式之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备通过阅读教科书来编程学校。这个问题我不明白,教科书也没有给出答案。



PS:我在网上学了一些C ++ / C#通过适当的教程编程类,所以我挣扎着一些概念。


问:对于以下每对scanf格式字符串,表示
两个字符串是否相等。如果他们不是,显示
如何区分他们。



A)%d%d

B)%d-%d-%d%d - %d - %d

C)%f%f

D)%f,%f vs %f,%f




首先,我甚至不明白问题在问什么。教科书是否意味着这两个字符串是否相等?

如果是的话,有人可以解释一下这些差异,并告诉我他们是如何区分的吗?解决方案 c>首先:%d vs %d,它们是 scanf()的等效格式字符串。



将执行以下操作。它永远不会失败。

1)扫描并放弃(跳过)可选的空格。

2)读取非空白或文件结束后,如果没有(EOF),读取的最后一个字符被放回到 stdin



% d本身会尝试3件事(可能会失败)

1)扫描并放弃(跳过)可选的空白区域

2)扫描并放弃转换表示十进制整数的数字文本。

3)读取非数字文本或文件结束后,如果不是(EOF),则读取的最后一个字符被放回到 stdin



%d这与刚刚用%d执行第二个操作的结果是一样的。 $ b

* scanf()说明符注意:


输入空格字符(由<$ c除非规范包含 [ c 函数)被跳过, ,或 n 说明符。 C11§7.21.6.28






B C D 差异?

鼠标悬停提示1:


A scanf()说明符之前,除了上面提到的3之外,它是一个等效的scanf()格式,没有它。

鼠标悬停提示2:


/ b>

鼠标悬停提示3:


考虑输入:

123 -456-789

123.456 x接下来要读取的字符是什么?




%d-%d-%d vs %d - %d - %d

C)%f%f

D)%f,%f与%f,%f



回答:


I'm currently prepping myself for programming school by going through the textbook. There's this particular question which I don't understand and the textbook doesn't give the answer.

PS: I've learned some C++/C# online, but never go through proper-taught programming classes, so I'm struggling with some of the concepts.

Q: For each of the following pairs of scanf format strings, indicate whether or not the two strings are equivalent. If they are not, show how they can be distinguished.

A) "%d" versus " %d"
B) "%d-%d-%d" versus "%d -%d -%d"
C) "%f" versus "%f "
D) "%f,%f" versus "%f, %f"

First off, I don't even understand what the question is asking. What does the textbook mean by whether or not the 2 strings are 'equivalent'?

If they are, could someone explain the differences and possibly show me how they can be distinguished?

解决方案

Let us try A first: "%d" versus " %d", they are equivalent format strings for scanf().

" " will do the following. It never fails.
1) Scan and discard (skip) optional white-space.
2) After reading a non-white-space or end-of-file, if not (EOF), the last character read is put back into stdin.

"%d" itself will attempt 3 things (It can fail)
1) Scan and discard (skip) optional white-space.
2) Scan and convert numeric text representing a decimal integer.
3) After reading a non-numeric text or end-of-file, if not (EOF), the last character read is put back into stdin.

" %d" does both the above. It is the same result of just doing the 2nd with "%d".

With *scanf() specifiers note:

Input white-space characters (as specified by the isspace function) are skipped, unless the specification includes a [, c, or n specifier. C11 §7.21.6.2 8


B, C, D differences?

Mouse over for hint 1:

A " " before a scanf() specifier, except the 3 noted above, is an equivalent scanf() format as without it.

Mouse over for hint 2:

Only 1 of 3 equivalent.

Mouse over for hint 3:

Consider inputs:
"123 -456-789"
"123.456 x" What is the next character to be read?

B) "%d-%d-%d" versus "%d -%d -%d"
C) "%f" versus "%f "
D) "%f,%f" versus "%f, %f"

Answer:

Awww, Do you really want to be given the answer?

这篇关于不同的scanf格式之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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