{"id":5466,"date":"2022-08-06T00:01:42","date_gmt":"2022-08-06T07:01:42","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=5466"},"modified":"2022-07-30T12:15:52","modified_gmt":"2022-07-30T19:15:52","slug":"going-mad-for-mod","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=5466","title":{"rendered":"Going Mad for Mod"},"content":{"rendered":"<p>I have two questions when it comes to using the <code>%<\/code> operator: Is it modulus or modulo? And which value comes first?<br \/>\n<!--more--><br \/>\nThe <em>modulus<\/em> is a number being manipulated.<\/p>\n<p><em>Modulo<\/em> is the operation, and the name of the <code>%<\/code> operator in C.<\/p>\n<p>If you want to confuse people, use both terms. A while back, I decided to use modulo only, but here&#8217;s a specific example to explain the difference and potentially confuse you further:<\/p>\n<p><code>10 % 3 = 1<\/code><\/p>\n<p>Read this statement as &#8220;ten modulo three equals one.&#8221; The number three is the modulus. Got it?<\/p>\n<p>Good! Now, never bring it up again.<\/p>\n<p>The next question I have is regarding the order of operations. The modulo (see?) returns the remainder of one value divided by another. (The second value is the modulus, which I&#8217;m bringing up again because I find it difficult to stick to my own rules.) So <code>10 % 3 = 1<\/code> because one is the remainder of ten divided by three. But <code>3 % 10<\/code> is three. In fact:<\/p>\n<p><code>A % B = A<\/code><\/p>\n<p>This expression holds true for all values of <code>B<\/code> greater than <code>A<\/code>. When <code>A<\/code> and <code>B<\/code> are equal, the result is zero. Otherwise, the result is the modulo of <code>B<\/code> against <code>A<\/code> when <code>A<\/code> is the larger value.<\/p>\n<p>Here is some code where the output may help you further understand the modulo operator and confirm that the largest value always comes first:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2022_08_06-Lesson.c\" rel=\"noopener\" target=\"_blank\">2022_08_06-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x,y;\r\n\r\n    for( x=1,y=10; y&gt;0; x++,y-- )\r\n        printf(\"%2d %% %2d = %d\\n\",x,y,x%y);\r\n\r\n    printf(\"%d %% %d = %d\\n\",5,10,5%10);\r\n    printf(\"%d %% %d = %d\\n\",2,8,2%8);\r\n    printf(\"%d %% %d = %d\\n\",7,1500,7%1500);\r\n    printf(\"%d %% %d = %d\\n\",4,4,4%4);\r\n    printf(\"%d %% %d = %d\\n\",10,5,10%5);\r\n    printf(\"%d %% %d = %d\\n\",8,2,8%2);\r\n    printf(\"%d %% %d = %d\\n\",1500,7,1500%7);\r\n    return(0);\r\n\r\n}<\/pre>\n<p>The <em>for<\/em> loop generates expressions with values cycling between one and 10. The other statements output specific values. Everything output appears as an expression:<\/p>\n<p><code>&nbsp;1 % 10 = 1<br \/>\n&nbsp;2 %&nbsp;&nbsp;9 = 2<br \/>\n&nbsp;3 %&nbsp;&nbsp;8 = 3<br \/>\n&nbsp;4 %&nbsp;&nbsp;7 = 4<br \/>\n&nbsp;5 %&nbsp;&nbsp;6 = 5<br \/>\n&nbsp;6 %&nbsp;&nbsp;5 = 1<br \/>\n&nbsp;7 %&nbsp;&nbsp;4 = 3<br \/>\n&nbsp;8 %&nbsp;&nbsp;3 = 2<br \/>\n&nbsp;9 %&nbsp;&nbsp;2 = 1<br \/>\n10 %&nbsp;&nbsp;1 = 0<br \/>\n5 % 10 = 5<br \/>\n2 % 8 = 2<br \/>\n7 % 1500 = 7<br \/>\n4 % 4 = 0<br \/>\n10 % 5 = 0<br \/>\n8 % 2 = 0<br \/>\n1500 % 7 = 2<\/code><\/p>\n<p>The first five lines of output confirm that <code>A % B = A<\/code>. When the values get larger, starting with <code>6 % 5<\/code>, you see the true modulo. Ditto for the other lines, when the second value is larger. Also for <code>4 % 4<\/code>, the result is zero in that four evenly divides into itself.<\/p>\n<p>Despite all the text I&#8217;ve inscribed into this post, I may still forget that the larger value comes first when crafting a modulo expression. But perhaps by writing some 368 words on the topic, it may finally stick in my brain.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been using the <code>%<\/code> operator for years, but I still need to remind myself of the proper order. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5466\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-5466","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5466"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5466\/revisions"}],"predecessor-version":[{"id":5478,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/5466\/revisions\/5478"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}