Log in

View Full Version : syntax in c???


Pattyta
30-03-2004, 09:21
does the symbol % represent remainder as in java ???? :confused:

thnx
rockie team

Chris Hibner
30-03-2004, 09:23
yes.

gnormhurst
30-03-2004, 10:48
% 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 ( 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 ( ii + 16 % 8 ) evaluates the same as (ii + 0) which is just (ii) Use parens to get what you want: ( (ii+16) % 8 ) It also makes for more readable code.

-norm