user-0 提问: C语言 标准问题

回复:
user: user-1
content: 引用 15 楼 vcf_reader 的回复:不同版本有不同的实现  不同的编译器对C标准的支持程度不同

user: user-2
content: 引用 15 楼 vcf_reader 的回复:不同版本有不同的实现

user: user-3
content: 不同版本有不同的实现

user: user-4
content: 引用 10 楼 wang0635 的回复:可以确定的。见C标准： When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. 如     printf("%d, %d\n", -5 / 2, -5 % 2);     printf("%d, %d\n", -5 / -2, -5 % -2);     printf("%d, %d\n", 5 / -2, 5 % -2); 输出： -2, -1 2, -1 -2, 1楼主这个就能看明白了把

user: user-5
content: 可以确定的。见C标准： When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. 如     printf("%d, %d\n", -5 / 2, -5 % 2);     printf("%d, %d\n", -5 / -2, -5 % -2);     printf("%d, %d\n", 5 / -2, 5 % -2); 输出： -2, -1 2, -1 -2, 1

user: user-6
content: 引用 8 楼 mymtom 的回复:如果被除数或除数为负数， / 和 % 操作符的结果不确定，所以尽量避免负数的这种运算； 如果确实需要，就要自己根据需要确定余数的符号，  “取余操作结果的正负由被除数决定” 我印象中有这么一句话，也一直这么认为的，因为目前接触的环境都是这个结果。。又一个误区。 看样子或许真跟平台有关了。多谢版主。 楼主可以给个总结。

user: user-7
content: 如果被除数或除数为负数， / 和 % 操作符的结果不确定，所以尽量避免负数的这种运算； 如果确实需要，就要自己根据需要确定余数的符号，

user: user-5
content: C99 中 6.5.5 引用 13 楼 mymtom 的回复:这明显是断章取义啊，就算你见过的所有编译器的结果都是是这样，并不能说明其他编译器的结果也是这样。 我不是道你的那几句话从哪里来的，我没有正式的C89标准，但是有一份C89草案里关于 % 操作符的完整描述是这样的：  3.3.5 Multiplicative operators  Syntax            multiplicative-expression:                   cast-expression                   multiplicative-expression *  cast-expression                   multiplicative-expression /  cast-expression                   multiplicative-expression %  cast-expression  Constraints     Each of the operands shall have arithmetic type.  The operands of the % operator shall have integral type.  Semantics     The usual arithmetic conversions are performed on the operands.     The result of the binary * operator is the product of the operands.     The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder.  In both operations, if the value of the second operand is zero, the behavior is undefined.     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 .

user: user-7
content: 这明显是断章取义啊，就算你见过的所有编译器的结果都是是这样，并不能说明其他编译器的结果也是这样。 我不是道你的那几句话从哪里来的，我没有正式的C89标准，但是有一份C89草案里关于 % 操作符的完整描述是这样的：  3.3.5 Multiplicative operators  Syntax            multiplicative-expression:                   cast-expression                   multiplicative-expression *  cast-expression                   multiplicative-expression /  cast-expression                   multiplicative-expression %  cast-expression  Constraints     Each of the operands shall have arithmetic type.  The operands of the % operator shall have integral type.  Semantics     The usual arithmetic conversions are performed on the operands.     The result of the binary * operator is the product of the operands.     The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder.  In both operations, if the value of the second operand is zero, the behavior is undefined.     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 .

user: user-7
content: 引用 10 楼 wang0635 的回复:可以确定的。见C标准： When integers are divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a. 如     printf("%d, %d\n", -5 / 2, -5 % 2);     printf("%d, %d\n", -5 / -2, -5 % -2);     printf("%d, %d\n", 5 / -2, 5 % -2); 输出： -2, -1 2, -1 -2, 1 这明显是断章取义啊，就算你见过的所有编译器的结果都是是这样，并不能说明其他编译器的结果也是这样。 我不是道你的那几句话从哪里来的，我没有正式的C89标准，但是有一份C89草案里关于 % 操作符的完整描述是这样的：  3.3.5 Multiplicative operators  Syntax            multiplicative-expression:                   cast-expression                   multiplicative-expression *  cast-expression                   multiplicative-expression /  cast-expression                   multiplicative-expression %  cast-expression  Constraints     Each of the operands shall have arithmetic type.  The operands of the % operator shall have integral type.  Semantics     The usual arithmetic conversions are performed on the operands.     The result of the binary * operator is the product of the operands.     The result of the / operator is the quotient from the division of the first operand by the second; the result of the % operator is the remainder.  In both operations, if the value of the second operand is zero, the behavior is undefined.     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 .

user: user-8
content: 两个符号不同的数相除 在程序中的规则 和 数学 中的规则是不一样的  数学中的规则 参考 带余除法，带余除法规定余数r >= 0  编程中 % 这个叫取模， 取模 != 取余  对于整型数a，b来说，取模运算或者求余运算的方法都是： 1.求商： c = a/b; 2.求模/余数： r = a - c*b. 求模运算和求余运算在第一步不同:  取模运算在取c的值时，向0 方向舍入(fix())；而取余运算在计算c的值时，向负无穷方向舍入(floor())。 例如：计算-7 / 4 那么：a = -7；b = 4； 第一步：求整数商c，c求出来是 -1.75。求模运算c = -1（向0方向舍入），求余c = -2（向负无穷方向舍入）； 第二步：计算模和余数，公式相同，但因c的值不同，求模时r = -3，求余时r = 1。

user: user-9
content: 你初中数学没学吗

user: user-0
content: 引用 6 楼 mymtom 的回复:[quote=引用 3 楼 call_from_dream 的回复:] 数学上怎样，编译器一般都会尊重数学结果的，除非发生内存溢出，精度不够什么的。毕竟，编译器也是根据实际问题的模型去解决问题的。个人认为：当a ＜ 0 ， b ＞０时，　ｑ不会为正，顶多是0，不然就是负数。 楼主都说了q的正负是由编译器决定的。 [/quote]  版主有没有一些建设性的意见？？

user: user-7
content: 引用 3 楼 call_from_dream 的回复:数学上怎样，编译器一般都会尊重数学结果的，除非发生内存溢出，精度不够什么的。毕竟，编译器也是根据实际问题的模型去解决问题的。个人认为：当a ＜ 0 ， b ＞０时，　ｑ不会为正，顶多是0，不然就是负数。 楼主都说了q的正负是由编译器决定的。

user: user-10
content: 数学上怎样，编译器一般都会尊重数学结果的，除非发生内存溢出，精度不够什么的。毕竟，编译器也是根据实际问题的模型去解决问题的。个人认为：当a ＜ 0 ， b ＞０时，　ｑ不会为正，顶多是0，不然就是负数。

user: user-0
content: 引用 1 楼 user-11 的回复:还有一个东西叫虚拟机呢。  虚拟机？？？　　Java吗？？？  C语言有什么虚拟机？？？

user: user-11
content: 还有一个东西叫虚拟机呢。