为什么我在float表达式中得到零,比如1/2? [英] Why I am getting zero in float expressions like 1/2?

查看:713
本文介绍了为什么我在float表达式中得到零,比如1/2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  #include< stdio.h> 
$ b $ int main(int argc,char ** argv){

printf(%f,(1/2));
返回0;


$ *输出:0.00000 * /


解决方案

1 / 不是浮点表达

  printf(%f,(1/2)); 

内部圆括号是不必要的。这是一个更容易阅读:

  printf(%f,1/2); 

在大多数情况下,C表达式的类型是由表达式本身决定的,而不是由上下文出现。这甚至适用于更大的表达式的子表达式



算术运算符 + - * / 总是取两个相同数字类型的操作数,这种类型的结果。有规则将操作数转换为通用类型,但是 1 2 都是 int ,所以我们不用担心这个。所有这些运算符(如果使用 int 操作数调用)都会产生 int 结果。整数除法截短,丢弃任何剩余部分,所以 1 / 产生了 INT 0



所以上面相当于:

  printf(%f,0); 

%f类型 double的参数; 0 类型为 int 。对于大多数函数来说,会有一个隐式的转换,但是参数的类型是由格式字符串决定的,而不是由函数声明决定的,所以编译器不知道要转换的类型。 (考虑格式字符串不一定是一个字符串字面值)用%f传递 int $ c>格式有未定义的行为。就你而言,恰巧打印 0 。我们可以推测如何发生,但没关系;你需要修改代码。



如果你想打印 int 值,你可以使用<$ c

$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ ;

但是您可能需要 0.5 。你可以通过使用 double 类型的操作数来获得它:

  printf %f,1.0 / 2.0); 



(你可以改变仅仅是两个操作数的浮点常量之一,但它是更清楚)



最后,您应该在输出结尾处打印换行符:

  printf(%f \ n,1.0 / 2.0); 


Can please someone explain this behavier :

#include <stdio.h>

int main(int argc, char **argv){

   printf("%f", ( 1 / 2 ) );     
   return 0;

} 

/* output : 0.00000 */

解决方案

1 / 2 is not a floating-point expression.

printf("%f", ( 1 / 2 ) );

The inner parentheses are unnecessary; this is a bit easier to read as:

printf("%f", 1 / 2);

In most cases, the type of an expression in C is determined by the expression itself, not by the context in which it appears. This applies even to subexpressions of larger expressions.

The arithmetic operators +, -, *, and / always take two operands of the same numeric type, and yield a result of that type. There are rules to convert the operands to a common type, but 1 and 2 are both of type int, so we needn't worry about that. All these operators, if invoked with int operands, yield an int result. Integer division truncates, discarding any remainder, so 1 / 2 yields the int value 0.

So the above is equivalent to:

printf("%f", 0);

The "%f" format requires an argument of type double; 0 is of type int. For most functions, there would be an implicit conversion, but the types of the parameters are determined by the format string, not by the function declaration, so the compiler doesn't know what type to convert to. (Consider that the format string doesn't have to be a string literal.) Passing an int argument with a "%f" format has undefined behavior. In your case, it just happened to print 0. We could speculate about how that happened, but it doesn't matter; you need to fix the code.

If you wanted to print that int value, you could use "%d":

printf("%d", 1 / 2);

But you probably want 0.5. You can get that by using operands of type double:

printf("%f", 1.0 / 2.0);

(You could change just one of the two operands to a floating-point constant, but it's clearer to change both.)

Finally, you should print a newline at the end of your output:

printf("%f\n", 1.0 / 2.0);

这篇关于为什么我在float表达式中得到零,比如1/2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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