Although I have already written many programs using modulo(modulus) division in the blog, yet i haven't explained it anywhere and relatively new programmers would be opening up some other reference pages to search what % sign means , I thought I can make life simpler for them including it here. Better late than never :)
So what a%b means in C
Say a = 35 and b = 8
35 when divided by 8 it gives quotient as 4 and remainder as 3.
In C language
35%8=3
24%4=0
We can also write it as 35=4*8+3
8%14?
8=0*14+8
Hence 8%14=8
(-5)modulo3?
possible.
-5-(3*-1)=-2 .
make the remainder +ve by adding
-2+3=1
(-5)modulo3=1
But in gcc compiler
-5%3=-2
and
5%-3=2
and
-5%-3=-2
So what a%b means in C
Say a = 35 and b = 8
35 when divided by 8 it gives quotient as 4 and remainder as 3.
In C language
35%8=3
24%4=0
We can also write it as 35=4*8+3
8%14?
8=0*14+8
Hence 8%14=8
(-5)modulo3?
possible.
-5-(3*-1)=-2 .
make the remainder +ve by adding
-2+3=1
(-5)modulo3=1
But in gcc compiler
-5%3=-2
and
5%-3=2
and
-5%-3=-2
No comments:
Post a Comment