C语言中整除问题

aerosjc 2013-02-01 02:50:44
C语言中,当除数、被除数和商都定义成int型时,如不能整除,商会自动转换成int类型。
那么,C语言是不是默认舍去小数部分,不论小数部分是否足够大?
如,10/3=3,舍去了小数部分;-10/3=-3,舍去了小数部分。
我试了试编译这两个例子,结果是这样。
对不对?我不太肯定
...全文
769 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
aerosjc 2013-02-01
  • 打赏
  • 举报
回复
引用 2 楼 mymtom 的回复:
引用 1 楼 rocktyt2 的回复:对于负数,不同的编译器处理方式似乎是不一样的 When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less……
使用CodeBlocks(GCC编译器)验证了一下,心里接受了。谢谢! 原来/和%是一起的。
N3verL4nd 2013-02-01
  • 打赏
  • 举报
回复
整数去尾 整形*整形=整形 整形/整形=整形
赵4老师 2013-02-01
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实!
雪碧透心凉_ 2013-02-01
  • 打赏
  • 举报
回复
如影随从 2013-02-01
  • 打赏
  • 举报
回复
是这样的,要是你定义的类型为int的。他就直接转换为int型。
mymtom 2013-02-01
  • 打赏
  • 举报
回复
上面的文字来自 C89 3.3.5 Multiplicative operators http://www.bsb.me.uk/ansi-c/ansi-c-one-file.html#sec_3_3_5
mymtom 2013-02-01
  • 打赏
  • 举报
回复
引用 1 楼 rocktyt2 的回复:
对于负数,不同的编译器处理方式似乎是不一样的
When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
rocktyt 2013-02-01
  • 打赏
  • 举报
回复
对于负数,不同的编译器处理方式似乎是不一样的

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧