我们为什么需要%C之前把空间? [英] Why we need to put space before %c?

查看:118
本文介绍了我们为什么需要%C之前把空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code,只要我编译它给人的离奇O / P。

The following code gives the bizarre o/p as soon as I compile it.

main() {
    char name[3];
    float price[3];
    int pages[3], i;

    printf ( "\nEnter names, prices and no. of pages of 3 books\n" ) ;

    for ( i = 0 ; i <= 2 ; i++ )
        scanf ("%c %f %d", &name[i], &price[i], &pages[i] );

    printf ( "\nAnd this is what you entered\n" ) ;

    for ( i = 0 ; i <= 2 ; i++ )
        printf ( "%c %f %d\n", name[i], price[i], pages[i] );
}

但是,如果我们屈服于%C前scanf函数语句的空间,它给适当的O / P。

But if we give the space in the scanf statement before %c, it gives proper o/p.

任何人都可以请解释一下我为什么会这样呢?

Can anyone please explain me why is it so?

更新: -

如果我提供这样的输入 -

If I am providing the input like this-

F
123.45
56
J
134
67
K
145
98

然后我的问题是,为什么不是我们%d个前给予%f和空间之前的空间?为什么我们需要给之前%的空间仅C?

then my question is why not we are giving space before %f and space before %d? Why we need to give space before %c only?

推荐答案

添加空格的格式字符串能够 scanf函数消耗从这种情况发生,每次输入换行符您preSS <大骨节病>返回。如果没有空间,名称[I] 将接收到的字符的'\\ n'真正的的字符是留给被%F

Adding the space to the format string enables scanf to consume the newline character from the input that happens everytime you press return. Without the space, name[i] will receive the char '\n', and the real char is left to be misinterpreted by %f.

所以,说你输入

a 1.0 2
b 3.0 4
c 5.0 6

该程序发现它更多的是这样的:

The program sees it more like this:

a 1.0 2\nb 3.0 4\nc 5.0 6\n\377

也就是说,换行符在文件中实际的字符(包括\\ 377这里指示文件结束)。

That is, the line-breaks are actual characters in the file (and the \377 here indicates "end of file").

第一个 scanf函数将出现做工精细,耗时一个char,浮点和整数。但它留下的输入是这样的:

The first scanf will appear to work fine, consuming a char, a float, and an integer. But it leaves the input like this:

\nb 3.0 4\nc 5.0 6\n\377

所以第二个 scanf函数将读的'\\ n'为%C,除非你摆脱它第一次。

So the second scanf will read the '\n' as its %c, unless you get rid of it first.

添加空格的格式字符串责成scanf函数放弃任何空白字符(任何空间标签'\\ T',或换行的'\\ n')。

Adding the space to the format string instructs scanf to discard any whitespace characters (any of space ' ', tab '\t', or newline '\n').

一个指令是以下之一:


      
  • 的空白字符的序列(空格,制表符,换行符等;看到isspace为(3))。该指令匹配的空白,包括没有任何金额,在输入。

  • A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.

...

http://linux.die.net/man/3/scanf

每当你使用 scanf函数%C 在一个循环中这样的问题出现了。因为,假设的自由格式的输入,换行可以在任何地方发生。所以,这是常见的尝试使用的两层的方法来避免整个问题。你读的的输入到缓冲区(使用与fgets );剁断傻换行符;然后用的sscanf 而不是 scanf函数的从缓冲区(串),而不是直从文件中读取。


This sort of problem arises whenever you use scanf with %c in a loop. Because, assuming free-form input, newlines can happen anywhere. So, it's common to try to avoid the whole issue by using a two-tiered approach. You read lines of input into a buffer (using fgets); chop-off the silly newline characters; then use sscanf instead of scanf to read from the buffer (string) instead of straight from the file.

这篇关于我们为什么需要%C之前把空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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