Operator (programming)

Operator (programming)

Programming languages generally support a set of operators that are similar to operations in mathematics. A language may contain a fixed number of built-in operators (e.g. + - * = in C and C++), or it may allow the creation of programmer-defined operators (e.g. Haskell). Some programming languages restrict operator symbols to special characters like + or := while others allow also names like div (e.g. Pascal).

Some built-in operators supported by a language have a direct mapping to a small number of instructions commonly found on central processing units, though others (e.g. '+' used to express string concatenation) may have complicated implementations.

The specification of a language will specify the precedence and associativity of the operators it supports. Languages which support programmer-defined operators require the specification of the precedence and associativity of new operator symbols (e.g. Prolog).

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 prefix, infix or postfix.

Syntactically operators usually contrast to functions. In most languages, functions may be seen as a special form of prefix operator with fixed precedence level and associativity, often with compulsory parentheses e.g. Func(a) (or (Func a) in LISP). Most languages support programmer-defined functions, but cannot really claim to support programmer-defined operators, unless they have more than prefix notation and more than a single precedence level. Semantically operators can be seen as special form of function with different calling notation and a limited number of parameters (usually 1 or 2).

A compiler can implement operators and functions with subroutine calls or with inline code.

Contents

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, 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 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.

Javascript follows opposite rules—finding the same expression above, it will convert the integer 12 into a string "12", then concatenate the two operands to form "123.14".

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.

Operator features in programming languages

The following table shows the operator features in several programming languages:

Programming language Nonalphanumeric operator symbols Alphanumeric operator symbols Prefix Infix Postfix Precedence Associativity Overloading Programmer-defined overloading Programmer-defined operator symbols
ALGOL 68 +* ** * / % %* %× - + < <= >= > = /= & -:= +:= *:= /:= %:= %*:= +=: :=: :/=:

(All operators have bold Alphanumeric equivalents, c.f. next column. Some have non ASCII equivalents, c.f. below.) ¬ +× ⊥ ↑ ↓ ⌊ ⌈ × ÷ ÷× ÷* □ ≤ ≥ ≠ ∧ ∨ ×:= ÷:= ÷×:= ÷*:= %×:= :≠:

not abs arg bin entier leng level odd repr round shorten i shl shr up down lwb upb lt le ge gt eq ne and or over mod elem minusab plusab timesab divab overab modab plusto is isnt Yes Yes No Yes (prefix operators always have priority 10) Infix operators are left associative, prefix operators are right associative Yes Yes Yes
C () [] -> . ! ~ ++ -- + - * & / % << >> < <= > <= == != ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= sizeof Yes Yes Yes Yes Yes Yes No No
C++ (more) sizeof new delete throw Yes Yes Yes Yes Yes Yes Yes No
Java new instanceof Yes Yes Yes Yes Yes Yes No No
Haskell + - * / ^ ^^ ** == /= > < >= <= && || >>= >> $ $! . ++ !! : Many more in common libraries Yes, by putting a function's name into backticks Yes Yes No Yes Yes Yes, using Type classes Yes
Pascal * / + - = < > <> <= >= := not div mod and or in Yes Yes No Yes Yes Yes No No
Seed7 {} [] -> ** ! + - * / << >> & >< | = <> > >= < <= <& := +:= -:= *:= /:= <<:= >>:= &:= @:= conv varConv parse conj div rem mdiv mod times mult in not and or digits lpad rpad lpad0 Yes Yes Yes Yes Yes Yes Yes Yes
Prolog  :- ?- ; , . =.. = \= < =< >= > == \== - + / * spy nospy not is mod Yes Yes Yes Yes Yes No No Yes
Lisp + - * / = /= < <= > >= mod rem floor truncate min max Yes (operator and parameters must be surrounded by parentheses) No No No No Yes No Yes
Smalltalk (yes - Up to two characters[1]) (yes - Needs a colon after the keyword) No Yes Yes No No Yes Yes Yes

See also

References


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Operator — may refer to: Contents 1 Music 2 Computers 3 Science and mathematics …   Wikipedia

  • Operator (disambiguation) — Operator can mean:* Operator, a type of mathematical function * Operator (biology), a segment of DNA regulating the activity of genes * Operator (extension), an extension for the Firefox web browser, for reading microformats * Operator (IRC), a… …   Wikipedia

  • Operator overloading — Theories and practice of polymorphism Double dispatch Multiple dispatch Operator overloading Polymorphism in computer science Polymorphism in OOP Subtyping …   Wikipedia

  • Operator (mathematics) — This article is about operators in mathematics. For other uses, see Operator (disambiguation). In basic mathematics, an operator is a symbol or function representing a mathematical operation. In terms of vector spaces, an operator is a mapping… …   Wikipedia

  • Programming by demonstration — (PbD) is a technique for teaching acomputer or a robot new behaviors by demonstrating the task totransfer directly instead of programming it through machinecommands.The term Programming by example (PbE) or Programming bydemonstration (PbD)… …   Wikipedia

  • Operator associativity — For the mathematical concept of associativity, see Associativity. In programming languages and mathematical notation, the associativity (or fixity) of an operator is a property that determines how operators of the same precedence are grouped in… …   Wikipedia

  • ?? Operator — The ?? operator, sometimes called the Coalescing Operator, is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, most notably C#.Conditional assignment?? is most frequently used to… …   Wikipedia

  • Programming idiom — A programming idiom is a means of expressing a recurring construct in one or more programming languages. Generally speaking, a programming idiom is an expression of a simple task or algorithm that is not a built in feature in the programming… …   Wikipedia

  • Relational operator — In computer science, a relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities. These include numerical equality (e.g., 5 = 5) and inequalities (e.g.,… …   Wikipedia

  • Common operator notation — This article is about the concept of operator precedence. For operator precedence parsing, see operator precedence parser. In programming languages, common operator notation is one way of notating mathematical expressions as a linear sequence of… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”