Operator (programming) |
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 Plus Plus. Operators in C are primitive operations of the language that the compiler can fairly directly map into the machine instructions of microprocessors.
In Haskell, any combination of symbols and punctuation can be used as a binary operator. The operation is defined like a function, and precedence and associativity can be set. Operators are a purely syntactic concept. An operator can be used as a function and vice versa by putting the name into parentheses or backticks respectively.
In certain programming languages, such as PostScript programming language, 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.
= 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.
Operators are binary or unary. 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, in 8 - 4 - 2, since subtraction in most languages is left associative, the so that expression evaluates left to right. 8 - 4 is evaluated first making restult 2, and not 6.
The ^ operator (sometime written **) is often right assosiative. So 4^3^2 equals 4^9, not 64^2.
: See Infix notation for more information about infix operator syntax.
= Operator overloading =
Main article: Operator overloading
In some programming languages an operator may work with more than one kind of data, (such as in Java programming language 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, such as C plus plus, one can define customized uses for operators; in Prolog, one can also define new 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 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.|
|