寻找问题

说干就干,于是我就跑到问答版本开始寻找问题,为了保证采纳率,我选转了一圈,选择了一个看着就比较小白的问题,哈哈,想着这不是手到擒来啊!初中的物理知识派上了用场了!

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_c语言


【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_赋值_02

问题处理

等等。。。摄氏度转华氏度咋转的。。。对不起初中物理老师。。。
百度一下吧。。。

摄氏温标(°C)和华氏温标(°F)之间的换算关系为:
F=C×1.8+32
C=(F-32)÷1.8

妥了,公式到手,然后我就把提问者的代码敲了一遍,看看他那里错了。

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_赋值_03

调试一波,还是真是32。

思考了一下:公式没问题啊。。。那就是C不对啊。加打印!

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_赋值_04


果然,C一直是0啊,那肯定是输入的姿势不对啊。

scanf其实好久没用了,还是刚开始学C的时候写写demo才用,有点模糊。。。

但是double咋用%f的啊,一般不是float使用%f的嘛。。。double虽然可以损失精度赋值给float,但是使用%f会不会异常啊?于是,百度了下scanf与double。。。果然double数应该使用%lf。。。立马修改调试一波

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_问题定位_05


妥了。小手一抖,红包到手!

后记

对于我们刚入门的小白,出了bug,应该优先添加打印信息或者F5调试,把问题定位切割定位,然后对于一些函数的参数列表要明确使用对象,不能想当然啊!

扩展篇 scanf

网上查了半天的scanf,各种表述的都有,不过最后发现还是官方原版的靠谱,内容最全,特别是涉及长度修饰的部分(例如double数输入,一般文档都不写的,只举例了float,要是知道double咋办,不好意思,请重新百度关键字)!就连我最爱的菜鸟编程都不全,哎。
附上英文的官方文档,需要深挖的同学可以直接去文末的官方链接去研究哈!

A format specifier for scanf follows this prototype:

%[*][width][length]specifier

Where the specifier character at the end is the most significant component, since it defines which characters are extracted, their interpretation and the type of its corresponding argument:

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_赋值_06


Except for n, at least one character shall be consumed by any specifier. Otherwise the match fails, and the scan ends there.The format specifier can also contain sub-specifiers: asterisk (*), width and length (in that order), which are optional and follow these specifications:

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_百度_07

This is a chart showing the types expected for the corresponding arguments where input is stored (both with and without a length sub-specifier):

【⛔C语言最熟悉的陌生人の你所不知道的scanf⛔】_百度_08


Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99.

权威官方http://www.cplusplus.com/reference/cstdio/scanf/