Operator (programming)
860030
221354144
2008-06-24T04:12:17Z
Mwtoews
711150
+see also to [[Relational operator]]
{{unref|date=July 2007}}
[[Programming languages]] generally support a set of '''operators''' that are similar to [[operator|operators in mathematics]]. A language may contain a fixed number of built-in operators (e.g., [[C programming language]]) or it may allow the creation of programmer defined operators (e.g., [[C++]]).
Most built-in operators supported by a language have a direct mapping to a small number of instructions (often one) commonly found on [[central processing units]].
The specification of a language will specify the [[precedence]] and [[associativity]] of the operators it supports.
Most programming language operators take one or two [[operands]], with a few supporting more operands (e.g., the ?: operator in C).
The position of the operator with respect to its operands may be [[Polish notation|prefix]], [[infix notation|infix]] or [[postfix notation|postfix]].
== Operator overloading ==
{{main|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 (programming language)|Java]] where the <tt>+</tt> 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.
== 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 <tt>12 + "3.14"</tt> producing the result of <tt>15.14</tt>. The text <tt>"3.14"</tt> 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.''
== Operators in APL ==
In the [[Kenneth E. Iverson|Iverson]] Notation that later became [[APL programming language|APL]], [[Kenneth Iverson|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.
==See also==
*[[Relational operator]]
[[Category:Programming constructs]]
[[fr:Opérateur (informatique)]]
[[he:אופרטור (תכנות)]]
[[ja:演算子]]
[[pl:Operator (programowanie)]]
[[ru:Операция (программирование)]]
[[uk:Оператор (програмування)]]