In C, how is modulus calculated for unsigned long?

c, math

Solution

Your prototype is for `function`, but you are calling `function1`. In this case, the `function1` function has no prototype, so the last parameter is considered to be an `int`. That's why you do not get the expected conversion.

Renaming the prototype should fix it:

void function1(int, int, unsigned int);

Problem

In my function, I first use mod as below: ``` void function(int, int, unsigned int); ``` calling: ``` function(100, 200, get_value() % 1024); ``` The `get_value()` will return a unsigned int that varies from `0x`0 to `0xffffffff`. In this situation `get_value() % 1024` may be a very big number larger than `1024` that causes `function()` to run too many times, so I changed it as below: ``` unsigned int num = get_value() % 1024; function(100, 200, num); ``` In this situation, `num` is ok. I used `gcc` to compile. So what is the difference between these two methods and how does C calcuate modulus for `unsigned long`?

Original source