RE: division by zero not an error for cc?
RE: division by zero not an error for cc?
- Subject: RE: division by zero not an error for cc?
- From: Jeff Howe <email@hidden>
- Date: Thu, 20 Sep 2001 12:51:26 -0400
>
>-----Original Message-----
>
From: Boz Jennings [mailto:email@hidden]
>
Sent: Thursday, September 20, 2001 11:55 AM
>
To: email@hidden
>
Subject: Re: division by zero not an error for cc?
>
>
>
Thanks for all the feedback.
>
>
I have run this and got output, "result is 0".
>
>
Originally the code was more like...
>
> int main(void) {
>
> int result, value = 0;
>
> result = 1 / value;
>
> printf( "%s%d\n",
>
> "result is ", result );
>
> return 0;
>
> }
>
... maybe I went to far simplifying it before posting.
>
>
Which all leads me to believe that maybe I need to look it up in the
>
ANSI standard. Is it available online somewhere? -- This all arose from
>
a C course I'm taking. The instructor says my machine, compiler or
>
something is whacked. Obviously, I'd love to prove him wrong! ;+)
From the Standard:
6.5.5 Multiplicative operators
Syntax
1 multiplicative-expression:
cast-expression
multiplicative-expression * cast-expression
multiplicative-expression / cast-expression
multiplicative-expression % cast-expression
Constraints
2 Each of the operands shall have arithmetic type. The operands of the
% operator shall have integer type.
Semantics
3 The usual arithmetic conversions are performed on the operands.
4 The result of the binary * operator is the product of the operands.
5 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.
Since in your case, the second operand is 0, you have undefined behavior.
Undefined behavior means just that: the compiler is free to do just about
anything, including returning 0, as it does in your case. It is your
program that is whacked, since it relies on undefined bahavior.
You can get the standard from the ISO organization at
http://www.iso.ch or
from the ANSI organization at www.ansi.org. Questions of this nature are
commonly answered on the comp.language.c newsgroup. This is also covered in
the C bible, The C Programming Language, by Kernighan and Ritchie (commonly
known as 'K&R'). Any good C reference should tell you the same thing.
regards,
Jeff
>
Peace,
>
boz (AKA Christofer Jennings)
>
_______________________________________________
>
cocoa-dev mailing list
>
email@hidden
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev