471,854 Members | 1,568 Online

# What does "%" mean in C 2
Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like:

flag = (i%primes[j]);

In this case, the variable 'i', 'j' and 'flag' are integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context?

Thanks
May 30 '07 #1
16 107147 AshishK
1 Hi. I am new to C programming and am working through some simple bits of code. Sometimes I come across a line like:

flag = (i%primes[j]);

In this case, the variable 'i', 'j' and 'flag' are integers and 'primes' is an array. I understand that '%' is used as a signifier in printf statements, e.g. where %d indicates a position for a decimal, but what does it mean in the above context?

Thanks
it is used of mod operator .
May 30 '07 #2
ilikepython
844 Expert 512MB
it is used of mod operator .
The modulo operator returns the remainder of the left side divided by the right side. So 5%2 will evalutate to 1. (5 / 2 = 2 remainder : 1).
May 30 '07 #3
102 Expert 100+
The modulo operator returns the remainder of the left side divided by the right side. So 5%2 will evalutate to 1. (5 / 2 = 2 remainder : 1).
Hi,
I just wanted to add % operator can be used only with "integers".

Regards,
May 31 '07 #4
Savage
1,764 Expert 1GB
Hi,
I just wanted to add % operator can be used only with "integers".

Regards,
And it deosn't make sense if divisor is 1,but I think we all know that.

:)

Savage
May 31 '07 #5
macmartin
2 Thanks everyone. I thought it would be something simple!
Jun 1 '07 #6
1,251 Expert 1GB
And it deosn't make sense if divisor is 1,but I think we all know that.

:)

Savage
It makes perfect sense, just not very useful. ;)

I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm.

Jun 1 '07 #7
Savage
1,764 Expert 1GB
It makes perfect sense, just not very useful. ;)

I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm.

I don't know how (e.g)5%1 can make perfect sense to u.

Savage
Jun 1 '07 #8
1,251 Expert 1GB
I don't know how (e.g)5%1 can make perfect sense to u.

Savage
5/1 = 5 R 0

so 5%1 = 0.

Actually n%1 = 0. Like I said, not very useful.

Jun 1 '07 #9
DeMan
1,806 1GB
I had thought that using a negitive as one of the paramters would give you a result that you might not expect, but I've just tested it and cannot determine what it was. Hmm
I've heard the negative modulus is badly implemented in some languages (not sure with the c/c++), although I'm not sure whether this is true it gives unexpected results because people don't really understand negative mod (and expect the wrong result).....

The main confusion stems from what should a negative modulus return?

Assuming negative numbers are fins in modular arithmetic
what is the remainder of -7/3?, most people would say 1, but it is in fact 2 (or -1, if you prefer it that way)
Essentially, modulus is an equivalence operator, that looks for "the equivalent of this number between zeero and n", thus for the -7%3 example above:
Expand|Select|Wrap|Line Numbers
1. let x= -7
2. while(x not between 0 and 3)
3. x=x+3;
4. endwhile
5. print x
6.
and x = 2;

if we change this to 7%-3, is the result the same?
Actually, no. we are looking for "the equivalent of this number between zero and n", that is, we wnat 7's equivalent between 0 and -3. To find this we must reduce 7 into the range, so subtracting 3 three times, we get -2.

To cut a long story short, the % operator gives unexpected results if you expect the wrong result ;)
Jun 1 '07 #10
1,251 Expert 1GB
I've heard the negative modulus is badly implemented in some languages (not sure with the c/c++), although I'm not sure whether this is true it gives unexpected results because people don't really understand negative mod (and expect the wrong result).....

The main confusion stems from what should a negative modulus return?

Assuming negative numbers are fins in modular arithmetic
what is the remainder of -7/3?, most people would say 1, but it is in fact 2 (or -1, if you prefer it that way)
Essentially, modulus is an equivalence operator, that looks for "the equivalent of this number between zeero and n", thus for the -7%3 example above:
Expand|Select|Wrap|Line Numbers
1. let x= -7
2. while(x not between 0 and 3)
3. x=x+3;
4. endwhile
5. print x
6.
and x = 2;

if we change this to 7%-3, is the result the same?
Actually, no. we are looking for "the equivalent of this number between zero and n", that is, we wnat 7's equivalent between 0 and -3. To find this we must reduce 7 into the range, so subtracting 3 three times, we get -2.

To cut a long story short, the % operator gives unexpected results if you expect the wrong result ;)
That might have been it, it doesn't look strange to me now, but it may have before. ;)

Jun 1 '07 #11
DeMan
1,806 1GB
Or I could be wrong, we'll find out soon enough
Jun 2 '07 #12
1,251 Expert 1GB
Or I could be wrong, we'll find out soon enough
While looking up the C99 standard, I found this.

Point 25 is probably what was confusing me and causing me grief as I was getting -22 % 7 == 6 instead of -1. Both are correct, but it confused me way back, though I'm not sure if I realised back then it was inconsistent between compilers.

Jun 2 '07 #13
DeMan
1,806 1GB
I stand corrected (at least in computing terms)...

Mathematically I think it makes more sense to insiast that the number is trubncated to between 0 and the divisor (that is negative if the divisor is negative, but positive if the divisor is positive).....And that the numerator is irrelevant interms of the sign of the modulus....

I can understand, though, that it is easier to define in terms of whether you shorten it toward 0 or allow shortening to plus/minus infinity as well.....
Jun 3 '07 #14
UberUdder
1 The implementation of the modulo operator to return something like this:

-22 % 7 = 6

makes all the same sense as why program language (except MATLAB) often use zero as the first index in an array.

In my opinion, the modulo operation has more applications to indexing than it does the mathematical sense of a modulus. This is because in C/C++ you are more likely to use modulo for pointers and counters.

Thus, the negative interpretation of a modulo is related to circular indexing. Take for example, you want to turn an array of data into a periodic array of data. This can be done identically by saying that data i=[0,6] goes from index [-6,13] would produce three repetitions of the data as would [0,20]. When using the modulo operator, -6 % 6 should wrap to zero and that the next element in the array should be -5 % 6 = 1.
Mar 30 '10 #15
onest30
1 Hello everyone,
anyone could please explain me what the below operation does?

mj %= MBIG;

Regards
Jun 28 '10 #16
donbock
2,425 Expert 2GB
These expressions:
Expand|Select|Wrap|Line Numbers
1. mj %= MBIG;
2. aj += ABIG;
are equivalent to these expressions:
Expand|Select|Wrap|Line Numbers
1. mj = mj % MBIG;
2. aj = aj + ABIG;
Jun 28 '10 #17

 2 posts views Thread by Steve Richter | last post: by 2 posts views Thread by Don | last post: by 3 posts views Thread by RobertTG | last post: by 6 posts views Thread by allenj | last post: by 58 posts views Thread by Larry David | last post: by 92 posts views Thread by Heinrich Pumpernickel | last post: by 45 posts views Thread by loudking | last post: by reply views Thread by NeoPa | last post: by reply views Thread by antdb | last post: by reply views Thread by Dolores Martín | last post: by 7 posts views Thread by Petrol | last post: by reply views Thread by aboka | last post: by 9 posts views Thread by CD Tom | last post: by 1 post views Thread by RonaldHiggins22 | last post: by 2 posts views Thread by DJRhino1175 | last post: by reply views Thread by isladogs | last post: by