473,386 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

What does "%" mean in C

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 108027
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
pradeep kaltari
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,
Pradeep
May 31 '07 #4
Savage
1,764 Expert 1GB
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
Thanks everyone. I thought it would be something simple!
Jun 1 '07 #6
AdrianH
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.


Adrian
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.


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

Savage
Jun 1 '07 #8
AdrianH
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.


Adrian
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
AdrianH
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. ;)


Adrian
Jun 1 '07 #11
DeMan
1,806 1GB
Or I could be wrong, we'll find out soon enough
Jun 2 '07 #12
AdrianH
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.


Adrian
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
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
Hello everyone,
anyone could please explain me what the below operation does?

mj %= MBIG;


Regards
Jun 28 '10 #16
donbock
2,426 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

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Steve Richter | last post by:
What does the "." mean in the following sql script stmts? use GO if exists (select * from dbo.sysobjects where id = object_id(N'.') and OBJECTPROPERTY(id,N'IsUserTable') = 1) drop table ....
2
by: Don | last post by:
What does the onfocus value of "this.select()" do in the following <input...> tag? <input type=text name="img" size=20 onfocus="this.select()" value="http://" tabindex=7> Thanks, Don ...
3
by: RobertTG | last post by:
Someone please translate the code below into English... Particularly the indicated line Thanks function attachComment() { var aForms = document.getElementsByTagName("FORM"); for (var i = 0;...
6
by: allenj | last post by:
DB21085I Instance "md" uses "32" bits and DB2 code release "SQL08012" with level identifier "02030106". Informational tokens are "DB2 v8.1.0.16", "s030508", "MI00048", and FixPak "2". Product is...
58
by: Larry David | last post by:
Ok, first of all, let's get the obvious stuff out of the way. I'm an idiot. So please indulge me for a moment. Consider it an act of "community service".... What does "64bit" mean to your friendly...
92
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
45
by: loudking | last post by:
Hello, all I don't quite understand what does ((time_t)-1) mean when I execute "man 2 time" RETURN VALUE On success, the value of time in seconds since the Epoch is retu rned. On error,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.