The % puzzles me.....

Discussion in 'C Programming' started by Don, Sep 22, 2003.

  1. Don

    Don Guest

    I have found a little code piece saying:

    int i,j;

    void main(){

    i=0; j=0;

    i = (i+1) % 32;
    j = (j+1) % 2;

    }

    what does this mean??? What do the % 32 and % 2 do?

    Best Regards
    Don
     
    Don, Sep 22, 2003
    #1
    1. Advertisements

  2. Don

    Ben Pfaff Guest

    main() always returns int in portable code.
    This is the "modulo" operator. It divides its first operand by
    its second and yields the remainder. The code fragments
    i = (i + 1) % 32;
    and
    i = i + 1;
    if (i >= 32)
    i = 0;
    are equivalent, given that `i' is never assigned a value greater
    than 31.

    You should refer to your C reference manual for more details
    (e.g. what happens when either operand is negative).
     
    Ben Pfaff, Sep 22, 2003
    #2
    1. Advertisements

  3. Good question, Don.

    It's simple. 5 % 2 means "the remainder of 5 divided by 2" (i.e., 1).

    0 0
    __ __
    i = 32|1 j = 2|1
    \ 0 \ 0
    \ - \ -
    --->1 -->1

    The C standard--in its index--calls % the "remainder operator."

    N869
    6.5.5 Multiplicative operators
    [#5]....The result of the % operator is the remainder [of
    the division of the first operand by the second]....If the
    value of the second operand is zero, the behavior is undefined.

    Here's a program you can run to test the values yourself.

    #include <stdio.h>

    int main()
    {
    int i, j;

    i = 0;
    j = 0;

    i = (i + 1) % 32;
    j = (j + 1) % 2;

    printf("i = %d, j = %d\n", i, j);

    return 0;
    }

    --Steve
     
    Steve Zimmerman, Sep 22, 2003
    #3
  4. Don

    Micah Cowan Guest

    Well, the "void main()" means undefined behavior. Don't do what you
    see here--it's not conforming C code. main() always returns an int.

    You are generally expected to have checked your C reference book
    before posting here; any decent reference should explain the %
    operator. But to answer your question, the expression

    a % b

    returns the remainder after dividing a by b. It will have the same
    sign as a (i.e., -5 % 32 is -5).

    -Micah
     
    Micah Cowan, Sep 22, 2003
    #4
  5. Who wrote this code ? Ask that person what did he/she intend to do
    with this code ?

    Anyway, % is the modulus operator in "c". This gives the remainder of
    "a divided by b".

    The c = a % b will return the remainder of the operation in the
    variable "c".

    Now, what this code is doing even i am not able to understand this...

    Regards,
    - Rahul Agarkar
     
    Rahul Agarkar, Sep 22, 2003
    #5
  6. Don

    olaf Guest

    Hello

    The % means the modulo of a divide

    5 % 2 = 1 => 5 / 2 = 2, 5 - (2*2) = 1
    5 % 3 = 2 => 5 / 3 = 1, 5 - (3*1) = 2

    it is bascily the rest of the integer devision.

    Greetings Olaf
     
    olaf, Sep 22, 2003
    #6
  7. int main() {
    By now you should know.

    I just want to say that you should perhaps prefer an unsigned int over a
    signed int for cycling between 0..(2**N-1). The reason is that some (many?)
    compilers may not deduce that this is what the code is intended for and
    thereby generate inefficient assembly code, rather than a simpler (and
    generally much more efficient) bitmask.

    Try disassembling the following functions and you might see what I mean.

    int mod32(int x) { return x % 32; }

    unsigned mod32u(unsigned x) { return x % 32; }

    The first function is likely to be much more costly (slower) than the second
    because the compiler must implement the semantics of the % operator, down to
    correct results for a negative operand.

    Whilst %32 is potentially more descriptive than &31, the results and
    efficiency can be affected by the type which the operators are being applied
    to.
     
    Peter Nilsson, Sep 22, 2003
    #7
  8. Don

    Al Bowers Guest

    The standard uses:
    (a/b)*b + a%b shall equal a.
    where a and b are integer types and
    b is not equal 0. (if the second operand, b, in a%b, is zero, you have
    undefined behavior)
    From this you can see that:
    (3/2)*2 + 3%2 = 3
    (3/2) equals 1
    1*2 + (3%2) = 3
    (3%2) = 3 - 2
    (3%2) = 1

    The integer types may be negative and the result may be
    negative, even negative zero for implementations that support
    a negative zero.
    -3%2 = -1
    -3%1 = 0 or (negative zero for implementations that support it).
     
    Al Bowers, Sep 22, 2003
    #8
  9. Don

    Villy Kruse Guest


    There is a difference between modulo and reminder when negative numbers
    gets involved. The reminder function is closely related to how you truncate
    integer division results.


    -5 % 3 = -2 => (-5) / 3 = (-1), (-5) - (3*(-1)) = (-5) - (-3) = (-2)
    -4 % 3 = -1
    -3 % 3 = 0
    -2 % 3 = -2
    -1 % 3 = -1
    0 % 3 = 0
    1 % 3 = 1
    2 % 3 = 2
    3 % 3 = 0
    4 % 3 = 1
    5 % 3 = 2

    -5 modulo 3 = 1
    -4 modulo 3 = 2
    -3 modulo 3 = 0
    -2 modulo 3 = 1
    -1 modulo 3 = 2
    0 modulo 3 = 0
    1 modulo 3 = 1
    2 modulo 3 = 2
    3 modulo 3 = 0
    4 modulo 3 = 1
    5 modulo 3 = 2


    Villy
     
    Villy Kruse, Sep 22, 2003
    #9
  10. Don

    Dan Pop Guest

    Ever considered reading a C book?

    Dan
     
    Dan Pop, Sep 23, 2003
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.