简体   繁体   English

为什么对float的char值赋值不起作用

[英]Why char value assignment to float is not working

Note:This question is very simple but when I am searching in Google I haven't got any clear clarification. 注意:这个问题非常简单,但是当我在谷歌搜索时,我没有得到任何明确的澄清。

I have following program 我有以下计划

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

o/p when I am giving a int value (e,g - 9) it is showing it as 9.000000 o / p当我给出一个int值(e,g - 9)时,它显示为9.000000

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. 但是当我给char值像'a'时它没有显示并显示0.000000。我知道memory representation of floatmemory representation of floatint完全不同但是当我给int value (9)它是如何显示但是当我是给char (a)也是一个int(97)没有显示。

How it is happening. 它是如何发生的。 What is the memory representation during char assignment. char赋值期间的内存表示是什么。

Note that there are no char s here anywhere in your code. 请注意,代码中的任何位置都没有char

This is the way scanf is supposed to work. 这是scanf 应该工作的方式。 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的返回值(就像你应该的那样!)你可能会看到它返回0,意味着没有匹配的项目。

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. 当你给scanf()一个"%f"格式字符串时,这意味着“我希望你尝试给我一个浮点数 。当你提供像'a'这样的输入时,它不会匹配任何东西,因为'a'不是有效的浮点数。

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

First, you usually should end your printf format string with a newline \\n or else call fflush . 首先,您通常应该使用换行符结束printf格式字符串\\n或者调用fflush

Then, you should read the documentation on scanf ie its man page scanf(3) 然后,您应该阅读scanf上的文档,即其手册页scanf(3)

It can fail, and it returns the number of successful matches: 它可能会失败,并返回成功匹配的数量:

Return Value 回报价值

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure. 这些函数返回成功匹配和分配的输入项的数量,这可以少于提供的数量,或者在早期匹配失败的情况下甚至为零。

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. 如果在第一次成功转换或匹配失败发生之前达到输入结束,则返回值EOF。 EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set indicate the error. 如果发生读取错误,也会返回EOF,在这种情况下,将设置流的错误指示符(请参阅ferror(3)),并设置errno指示错误。

A failure is happening when you type an input letter a against a %f conversion specification. 当您根据%f转换规范键入输入字母a时,会发生故障。 In your case the c variable is left unchanged (ie "uninitialized"). 在您的情况下, c变量保持不变(即“未初始化”)。

You should test the result of scanf (it is <=0 on failure in your case). 你应该测试scanf的结果(在你的情况下它是<=0 )。

scanf in the form you wrote it will try to find and read numbers ( int / float ) up to the first non numerical character (newline, space, any letter). scanf以你写的形式将尝试查找和读取数字( int / float )直到第一个非数字字符(换行符,空格,任何字母)。 Letters are int s, but this is because of the scanf and the way it should behave. 字母是int ,但这是因为scanf及其应该表现的方式。 Read up the docs. 阅读文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM