为什么char值分配浮动不工作 [英] Why char value assignment to float is not working

查看:112
本文介绍了为什么char值分配浮动不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:这个问题很简单,但是当我在谷歌正在寻找我没有得到任何明确的澄清

Note:This question is very simple but when I am searching in Google I haven't got any clear clarification.

我有以下程序

int main()
{
 float c;
 scanf("%f",&c);
 printf("%f",c);
}

O / P
当我给一个 INT 值(E,G - 9)它显示它作为9.000000

o/p when I am giving a int value (e,g - 9) it is showing it as 9.000000

但是当我给char值像'一'它没有显示,并显示0.000000.I知道浮法的内存重新presentation是完全不同的 INT 但再怎么当我给 int值(9)它显示,但是当我给 CHAR(一)这也是一个int(97)未显示。

but when I am giving char value like 'a' it is not showing and showing 0.000000.I know the memory representation of float is totally different from int but then how when I am giving int value (9) it is showing but when I am giving char (a) which is also an int (97) is not showing.

它是如何发生的。什么是字符分配期间重新presentation内存。

How it is happening. What is the memory representation during char assignment.

推荐答案

请注意,有没有字符的任何地方在这儿你code。

Note that there are no chars here anywhere in your code.

这是道路 scanf函数 是的应该的工作。如果您检查从 scanf函数的返回值的(像你应该的!)的你可能会看到它返回0,这意味着没有任何项目相匹配

This is the way scanf is supposed to work. If you check the return value from scanf (like you should be!) you'll probably see that it's returning 0, meaning no items were matched.

当你给 scanf()的 A %F格式字符串,这意味着我要你尝试并获得我的浮点的数量。当你提供像'A'输入,它不会匹配任何东西,因为'A'是不是有效的浮点数。

When you give scanf() a "%f" format string, that means "I want you to try and get me a floating point number. When you provide input like 'a', it's not going to match anything, because 'a' is not a valid floating-point number.

http://www.cplusplus.com/reference/cstdio/scanf/

这篇关于为什么char值分配浮动不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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