Thread: syntax in c???
View Single Post
  #3   Spotlight this post!  
Unread 30-03-2004, 10:48
gnormhurst's Avatar
gnormhurst gnormhurst is offline
Norm Hurst
AKA: gnorm
#0381 (The Tornadoes)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Trenton, NJ
Posts: 138
gnormhurst will become famous soon enoughgnormhurst will become famous soon enough
Re: syntax in c???

% is usually called the "mod" or "modulus" operator, but that means the same thing as remainder.

A couple of things to be careful of. The behavior of % with negative operands is not standardized. In
Code:
( a % b )
if a or b is negative, C only guarantees that the absolute value of the result will be less than b. Better make sure the numbers are positive. One way to do this is to add a multiple of the second operand to the first operand. But be careful of precedence.

The order of precedence of '%' is the same as for '*' and '/', which is higher than for '+' and '-'. So
Code:
( ii + 16 % 8 )
evaluates the same as
Code:
(ii + 0)
which is just
Code:
(ii)
Use parens to get what you want:
Code:
( (ii+16) % 8 )
It also makes for more readable code.

-norm
__________________
Trenton Tornadoes 381
2004 Philadelphia Regional Winners
2006 Xerox Creativity Award
---
My corner of the USPTO.
My favorite error message from gcc: main is usually a function
My favorite error message from Windows: There is not enough disk space available to delete this file.