怪异的行为scanf函数读取时数和新行 [英] Weird scanf behaviour when reading number and newline

查看:249
本文介绍了怪异的行为scanf函数读取时数和新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚意识到这scanf函数的'错误',现在经过8年以下。

I just realize this 'bug' of scanf now after 8 years with C.

下面scanf函数code将输入的第二行跳过前导空白字符。

Below scanf code will skip the leading whitespace characters from the second line of input.

int x;
char in[100];

scanf("%d\n",&x);
gets(in);

输入:

1
     s

X 将包含 1 ,但将只是不是

x will contain 1, but in will be just "s" not " s"

这是标准C或只是GCC的行为?

Is this standard C or just gcc behaviour?

推荐答案

scanf函数格式字符串中的空格字符会导致 scanf函数至非空白字符发生消耗任何(和所有)的空白。

A whitespace character in your scanf format string will cause scanf to consume any (and all) white space till a non-whitespace char occurs.

这似乎是标准的 scanf函数行为,而不是仅限于海湾合作委员会。

This seems to be standard scanf behaviour and is not limited to gcc.

这篇关于怪异的行为scanf函数读取时数和新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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