455,515 Members | 1,848 Online Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

# What does "%" mean in C

 P: 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 Replies

 P: 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

 Expert 100+ P: 844 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

 Expert 100+ P: 102 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, Pradeep May 31 '07 #4

 Expert 100+ P: 1,764 Hi, I just wanted to add % operator can be used only with "integers". Regards, Pradeep And it deosn't make sense if divisor is 1,but I think we all know that. :) Savage May 31 '07 #5

 P: 2 Thanks everyone. I thought it would be something simple! Jun 1 '07 #6

 Expert 100+ P: 1,251 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. Adrian Jun 1 '07 #7

 Expert 100+ P: 1,764 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. Adrian I don't know how (e.g)5%1 can make perfect sense to u. Savage Jun 1 '07 #8

 Expert 100+ P: 1,251 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. Adrian Jun 1 '07 #9

 100+ P: 1,806 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 let x= -7 while(x not between 0 and 3) x=x+3; endwhile print x   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

 Expert 100+ P: 1,251 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 let x= -7 while(x not between 0 and 3) x=x+3; endwhile print x   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. ;) Adrian Jun 1 '07 #11

 100+ P: 1,806 Or I could be wrong, we'll find out soon enough Jun 2 '07 #12

 Expert 100+ P: 1,251 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. Adrian Jun 2 '07 #13

 100+ P: 1,806 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

 P: 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

 P: 1 Hello everyone, anyone could please explain me what the below operation does? mj %= MBIG; Regards Jun 28 '10 #16

 Expert 100+ P: 2,415 These expressions: Expand|Select|Wrap|Line Numbers mj %= MBIG; aj += ABIG; are equivalent to these expressions: Expand|Select|Wrap|Line Numbers mj = mj % MBIG; aj = aj + ABIG; Jun 28 '10 #17 