用C简单的前​​pression怪异输出,为什么呢? [英] Weird output of simple expression in C, why?

查看:99
本文介绍了用C简单的前​​pression怪异输出,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TURBOC ++。我写的这是不是导致正确的评价如下EX pression,我错过了一些概念背后?

  INT C = 300 *300分之300;
的printf(%D,C);

的输出是


  

81


为什么?


解决方案

  300 * 300 90000。

假设INT是16bit,你已经溢出。

溢出环绕,让您: 24464

 300分之24465= 81.55

不要依赖于这一点。这是未定义的行为。

I am using TurboC++. I write the following expression which is not resulting in proper evaluation, am I missing some concept behind it?

int c=300*300/300;
printf("%d",c);

The output is

81

Why?

解决方案

300*300 is 90000.

Assuming int is 16bit, you have overflowed.

The overflow wraps around, giving you: 24464.

24465/300 = 81.55

Do not rely on this. It is undefined behavior.

这篇关于用C简单的前​​pression怪异输出,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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