# Is there any reason why the modulo operator is denoted as %?

I would like to know if there is any reason why many programming languages use the notation % for the modulo operator?

It is used in the most "famous" languages:

• C
• C++
• C#
• Go
• Java
• Julia
• Lua
• Perl
• Python
• The symbol $%$ looks like the division $/$ and it is only one character long.
– plop
Dec 16 '20 at 11:25
• Interesting question, I don't know. @plop is that only a guess?
– Juho
Dec 16 '20 at 12:17
• is javascript not famous? Dec 18 '20 at 8:00
• @Rainb I think that you are confusing famous and infamous. Dec 18 '20 at 18:16
• Is this a computer science question? Surely something like "retrocomputing" would be a better place for the history and development of notation. Dec 18 '20 at 22:28

The earliest known use of % for modulo was in B, which was the progenitor of C, which was the ancestor (or at least godparent) of most languages that do the same, hence the operator's ubiquity.

Why did Thompson and Richie pick %? It had to be a printable ASCII character that wouldn't conflict with B's other features. % was available, and it resembles the / division operator, making it the obvious choice.

p.s. the creator of ASCII invented \ to represent "reverse division", so it wasn't a candidate for modulo.

• what is a reverse division? multiplication? Dec 17 '20 at 5:46
• a \ b = b / a. You can find this usage in sage for matrices, likely borrowed from somewhere else. Dec 17 '20 at 8:20
• Note: for matrices in Matlab a\b isn't normally written b / a - it's normally written as (a)^1*b, which is different as matrix multiplication isn't commutative. Dec 17 '20 at 16:58
• Is there any direct evidence that Bemer intended \ to represent reverse division? The page here does not mention division at all. It seems he just wanted a character with a diagonal line. Dec 18 '20 at 13:56
• According to citeseerx.ist.psu.edu/viewdoc/… Bemer wrote it in the June 1960 issue of Computing Reviews by ACM. A handful of academic libraries have physical copies, but there doesn't appear to be a digital version. Dec 18 '20 at 23:15

This is very likely a historical development. Looking at this table, we see that C was likely the first language to use % for modulo. Its predecesor BCPL used rem, and older languages such as Fortran, Algol, Lisp, and Cobol did not use %. But that's just my uninformed guess.

• The "B" language which "C" replaced did also use it. See en.wikipedia.org/wiki/B_(programming_language). Looking at the manual (linked from that page), it says explicitly "The operator % denotes modulo." This strongly suggests Ken Thompson or Dennis Ritchie as the originator of the convention, since they designed B. UNIX shell script also uses % for modulo, although it's possible that scripting was not advanced enough to need it back in the early 70s. Dec 16 '20 at 15:46
• Very good. So BCPL didn't have it, but already B did. Wasn't B rather short-lived? Dec 16 '20 at 15:53
• Yes, it was invented for B, then copied to C. Over half of the languages on the list above are direct descendants of C. And the others are either indirect descendants, or (like many languages) just copied a lot of their operators from C-descendant languages. Dec 16 '20 at 16:59
• It seems to be pretty common for the answer to "Why does C do X?" to be "Because of B". Dec 17 '20 at 15:40

I would like to know if there is any reason why many programming languages use the notation % for the modulo operator?

I would strongly suspect it is because both Unix and Windows (the two operating system families that have survived to the present day), chose C as their main programming language. The result is most of the programming languages we use today, were designed in environments where C and/or C++ were the dominant "systems" programming language.

People designing a new language will undoubtedly have aspects that they wish to change from previous languages, but there will also be aspects that they think are fine the way they are or at least aspects they can't think of a better solution for. Development of mainstream programming languages has been more evolutionary than revolutionary.

The result has been that most successful programming languages over the past few decades have been directly or indirectly influenced by C. Lets look at the languages on your list in chronological order and see what environments they were developed in and/or what languages were used to develop them.

• C++: designed by Bjarne Stroustrup while working on Unix at AT&T in 1979 as an object orientated extension of C.
• Perl: Created by Larry Wall in 1987 while working at Unisys. Perl is written in C and it's clear from the initial announcement newsgroup post that perl was designed to be used on a Unix system.
• Python: Created by Guido van Rossum around 1989 while working at a university. Again Python is written in C and was written with Unix, Windows, Mac and an experimental OS the university was working on in mind.
• Lua: Created at a Brazilian University in 1993. It was written in C, though there were certainly many influences from other languages. It's probably the least C like of the languages here.
• Java: designed by Sun Microsystems (a Unix vendor) in 1995. Largely implemented in C++ and borrows much of it's basic syntax from there.
• C#: designed by Microsoft in 2000 as a substitute for Java after they fell out with Sun over MS-specific extensions to Java.
• GO: Designed at Google in 2007 in reaction to problems Google engineers percived in C, C++ and Python, borrows most of it's syntax from C.
• Julia: Developed in 2009, targetting Linux, Windows and Macos, appears that the non-julia parts are written in C.
• Should mention Pascal - which uses the % operator, but unlike the others on the list does not appear to have its roots in C, coming from the ALGOL family instead. (I don't believe ALGOL used %, but there are many varieties of it, so maybe some do?) Dec 17 '20 at 17:38
• Umm, I don't know what variant of pascal you are using, but the ones I'm familiar with don't use % for modulo, they use it as a prefix for binary numbers. Dec 17 '20 at 17:50
• Hmm, might be a modern variant? I just looked it up at this site Dec 17 '20 at 19:09
• @DarrelHoffman Looks like it might just be a mistake: in the precedence table at the bottom of the page it lists *, /, div, mod, and, & suggesting that mod not % is the modulo operator. Dec 18 '20 at 14:41