26

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
zdm
  • 1,056
  • 9
  • 15

3 Answers3

45

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.

Foo Bar
  • 536
  • 4
  • 6
13

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.

Andrej Bauer
  • 31,657
  • 1
  • 75
  • 121
2

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.
Peter Green
  • 235
  • 1
  • 5