scanf 使用 %f 表示整数
我正在尝试解析一个由空格分隔的数字的字符串。但是,这些数字可以是整数或浮点数。 scanf 中有没有一种方法可以将整数和数字解析为一个集体浮点数?
例如:
float arg1, arg2 = 0;
sscanf("LINE 10 10", "LINE %f %f", &arg1, &arg2);
// and
sscanf("LINE 10.0 10.0", "LINE %f %f", &arg1, &arg2);
I am trying to parse a string that has some numbers seperated by a space. However, these numbers could be integers or floats. Is there a way in scanf to parse both integers and numbers as one collective float?
For example:
float arg1, arg2 = 0;
sscanf("LINE 10 10", "LINE %f %f", &arg1, &arg2);
// and
sscanf("LINE 10.0 10.0", "LINE %f %f", &arg1, &arg2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它只是有效的,整数是浮点数的特殊情况。
但请注意,大整数可能无法用浮点数精确表示(例如
1e9 + 1
),但我认为您不必为此担心。It just works, integers are special cases of floating points.
Note, however, that large integers might not be able to be represented precisely by floats (eg.
1e9 + 1
), but I don't think you bother about that.事实证明我的测试是错误的,你可以用 %f 读取整数。
Turns out my tests were wrong, you can read from an integer with %f.