Operator (programming)
From Wikipedia, the free encyclopedia
This article or section is missing citations or needs footnotes. Using inline citations helps guard against copyright violations and factual inaccuracies. (December 2006) |
This article needs additional citations for verification. Please help improve this article by adding reliable references. Unsourced material may be challenged and removed. (July 2007) |
Programming languages generally have a set of operators that are similar to operators in mathematics: they are somehow special functions. In addition to arithmetic operations they often perform boolean operations on truth values and string operations on strings of text. Relational operators are used to test two values, such as equality if one value is greater than another. Unlike functions, operators often provide the primitive operations of the language, their name consists of punctuation rather than alphanumeric characters, and they have special infix syntax and irregular parameter passing conventions. The exact terminology, however, varies from language to language.
Conventionally, the computing usage of "operator" goes beyond the set of common arithmetic operators. The C programming language, for example, also supports operators like &, ++ and sizeof. Operators like sizeof, which are alphanumeric rather than a mathematical symbol or a punctuation character, are sometimes called named operators. See Operators in C and C++. Operators in C are primitive operations of the language that the compiler can fairly directly map into the machine instructions of microprocessors.
On the other hand, in languages such as Haskell and Prolog, operators are a purely syntactic concept. Any combination of symbols and punctuation can be used as an operator, and its precedence and associativity can be set. While Haskell only allows new binary operators, Prolog allows one to define operators that are either unary or binary and either prefix, infix or postfix. In Haskell, the operator can be defined and applied just like a function and vice versa by the appropriate use of parentheses or backquotes. Analogously in Prolog, terms use either operators or normal functors and quotes convert between the usages.
In certain programming languages, such as PostScript, the use of the word "operator" has more specific meaning, in that an operator is an executable element in the stack. Because operators here are always written postfix, the need for parentheses is redundant as the way objects are taken from the stack ensures correct evaluation. This is an example of Reverse Polish notation.
Contents |
[edit] Operator syntax
Computers are mathematical devices, but compilers and interpreters require a full syntactic theory of all operations in order to parse formulae involving any combinations correctly. In particular they depend on operator precedence rules, on order of operations, that are tacitly assumed in mathematical writing.
Most operators are binary or unary, although operators with higher arity such as the ternary operator exist. Binary operators ("bi" as in "two") have two operands. In "A*B" the * operator has two operands: A and B. In "!B" the "!" operator (meaning boolean NOT) has only one operand, and is therefore a unary operator. The "-" and "+" operators can be both binary and unary, in "-4" or "+4" it denotes a negative or a positive number, in "0-4" it acts as the subtraction operator.
Operators have associativity. This defines the correct evaluation order if a sequence of the same operator is used one after another: whether the evaluator will evaluate the left operations first or the right. For example, since subtraction in most languages is left associative, the expression 8 - 4 - 2 evaluates left to right: 8 - 4 is evaluated first, and the result is 2 (rather than 6).
The exponentiation operator (typically written as ^ or **) is often right associative. So 4^3^2 equals 4^9, not 64^2.
- See Infix notation for more information about infix operator syntax.
[edit] Operator overloading
In some programming languages an operator may be ad-hoc polymorphic, that is, have definitions for more than one kind of data, (such as in Java where the + operator is used both for the addition of numbers and for the concatenation of strings). Such an operator is said to be overloaded. In languages that support operator overloading by the programmer but have a limited set of operators, such as C++, operator overloading is often used to define customized uses for operators.
[edit] Operand coercion
Some languages also allow for the operands of an operator to be implicitly converted or coerced to suitable data types for the operation to occur. For example, in Perl coercion rules lead into 12 + "3.14" producing the result of 15.14. The text "3.14" is converted to the number 3.14 before addition can take place. Further, 12 is an integer and 3.14 is either a floating or fixed-point number (a number that has a decimal place in it) so the integer is then converted to a floating point or fixed-point number respectively.
In the presence of coercions in a language, the programmer must be aware of the specific rules regarding operand types and the operation result type to avoid subtle programming mistakes.
- See Type conversion for more information about coercion.
[edit] Operators in APL
In the Iverson Notation that later became APL, Kenneth E. Iverson defined several operators (reduction, inner product, outer product) acting on functions to produce functions. So, for example, +/ (plus-reduce) applies the reduction operator to the binary function + to create a function for adding vectors, rows of matrices, etc. thus:
+/1 2 3 4 10
Many more operators (Scan, Each, Curry,...) have been defined since then in various dialects of APL, and user-defined operators have also been added in those dialects of APL that provide nested arrays.