Operator (computer programming)

Basic programming language construct


title: "Operator (computer programming)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["operators-(programming)", "programming-constructs"] description: "Basic programming language construct" topic_path: "general/operators-programming" source: "https://en.wikipedia.org/wiki/Operator_(computer_programming)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0

::summary Basic programming language construct ::

::callout[type=note] operators in computer programming ::

In computer programming, an operator is a programming language construct that provides functionality that may not be possible to define as a user-defined function (e.g. sizeof in C) or has syntax different from that of a function (e.g. infix addition as in a+b). Like other programming language concepts, operator has a generally accepted, although debatable, meaning among practitioners.

Some operators are represented with symbols characters typically not allowed for a function identifier to allow for presentation that is more familiar looking than typical function syntax. For example, a function that tests for greater-than could be named gt, but many languages provide an infix symbolic operator so that code looks more familiar. For example, this:

if gt(x, y) then return

Can be:

if x y then return

Some languages allow a language-defined operator to be overridden with user-defined behavior and some allow for user-defined operator symbols.

Operators may also differ semantically from functions. For example, short-circuit Boolean operations evaluate later arguments only if earlier ones are not false.

Differences from functions

Syntax

Many operators differ syntactically from user-defined functions. In most languages, a function is prefix notation with fixed precedence level and associativity and often with compulsory parentheses (e.g. Func(a) or (Func a) in Lisp). In contrast, many operators are infix notation and involve different use of delimiters such as parentheses.

In general, an operator may be prefix, infix, postfix, matchfix, circumfix or bifix, and the syntax of an expression involving an operator depends on its arity (number of operands), precedence, and (if applicable), associativity. Most programming languages support binary operators and a few unary operators, with a few supporting more operands, such as the ?: operator in C, which is ternary. There are prefix unary operators, such as unary minus -x, and postfix unary operators, such as post-increment x++; and binary operations are infix, such as x + y or x = y. Infix operations of higher arity require additional symbols, such as the ternary operator ?: in C, written as a ? b : c – indeed, since this is the only common example, it is often referred to as the ternary operator. Prefix and postfix operations can support any desired arity, however, such as 1 2 3 4 +.

Semantics

The semantics of an operator may significantly differ from that of a normal function. For reference, addition is evaluated like a normal function. For example, x + y can be equivalent to a function add(x, y) in that the arguments are evaluated and then the functional behavior is applied. However, assignment is different. For example, given a = b the target a is not evaluated. Instead its value is replaced with the value of b. The scope resolution and element access operators (as in Foo::Bar and a.b, respectively, in the case of e.g. C++) operate on identifier names; not values.

In C, for instance, the array indexing operator can be used for both read access as well as assignment. In the following example, the increment operator reads the element value of an array and then assigns the element value. ::code[lang=c] ++a[i]; ::

The C++ ` ::code[lang=cpp] cout << "Hello" << " " << "world!" << endl; ::

ad hoc polymorphic

Some languages provide operators that are ad hoc polymorphic inherently overloaded. For example, in Java the operator sums numbers or concatenates strings.

Customization

Some languages support user-defined overloading (such as C++ and Fortran). An operator, defined by the language, can be overloaded to behave differently based on the type of input.

Some languages (e.g. C, C++ and PHP) define a fixed set of operators, while others (e.g. Prolog, F#, OCaml, Haskell) allow for user-defined operators. Some programming languages restrict operator symbols to special characters like or while others allow names like [div](integer-division-division-of-integers) (e.g. Pascal), and even arbitrary names (e.g. Fortran where an upto 31 character long operator name is enclosed between dots).

Most languages do not support user-defined operators since the feature significantly complicates parsing. Introducing a new operator changes the arity and precedence lexical specification of the language, which affects phrase-level lexical analysis. Custom operators, particularly via runtime definition, often make correct static analysis of a program impossible, since the syntax of the language may be Turing-complete, so even constructing the syntax tree may require solving the halting problem, which is impossible. This occurs for Perl, for example, and some dialects of Lisp.

If a language does allow for defining new operators, the mechanics of doing so may involve meta-programming specifying the operator in a separate language.

Operand coercion

Some languages implicitly convert (aka coerce) operands to be compatible with each other. For example, Perl coercion rules cause 12 + "3.14" to evaluate to 15.14. The string literal "3.14" is converted to the numeric value 3.14 before addition is applied. Further, 3.14 is treated as floating point so the result is floating point even though 12 is an integer literal. JavaScript follows different rules so that the same expression evaluates to "123.14" since 12 is converted to a string which is then concatenated with the second operand.

In general, a programmer must be aware of the specific rules regarding operand coercion in order to avoid unexpected and incorrect behavior.

Examples

;Mathematical operators

;Program structure operators

;Conditional operators

;Notable C and C++ operators

Compound operator ;Compound operators

Operator features in programming languages

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

::data[format=table]

LanguageSymbolic operatorsAlphanumeric operators
ALGOL 68 each symbolic operator has an alphanumeric equivalent and some a non-ASCII equivalent
APL(requires ⎕ prefix)
B
C{{code1=() [] - . ! ~ ++ -- + - * & / % = == != ^
C++(same as C)(same as C plus) [typeid](typeid) [new](new-c) [delete](delete-c) [throw](exception-handling) [decltype](decltype) [static_cast](static-cast) [dynamic cast](dynamic-cast) [reinterpret_cast](reinterpret-cast) [const_cast](const-cast)
C#(same as C plus) [?.]() ?[] [??](null-coalescing-operator) ??=[sizeof](sizeof) nameof new stackalloc await [throw](exception-handling) checked unchecked is as delegate default true false
LINQ: from select where group...by group...by...into join...in...on...equals join...in...on...equals...into orderby orderby...descending
Roslyn-only: __makeref __refvalue __reftype
Java(same as C)[new](java-syntax-instantiation) [throw](exception-handling) [instanceof](instanceof)
Eiffel[] + - * / // = /= not and or implies "and then" "or else"
Haskell`+ - * / ^ ^^ ** == /= == $ $! . ++ !! :` (and many more)
mvBasic Databasic/Unibasic`+ - * / ^ ** : = ! & [] += -= := # = = #AND OR NOT EQ NE LT GT LE GE MATCH ADDS() ANDS() CATS() DIVS() EQS() GES() GTS() IFS()
Pascal* / + - = = :=[not](negation-programming) [div](integer-division-division-of-integers) [mod](modulo-operation) [and](logical-conjunction) [or](logical-disjunction) in
Perl^ &&' '' `
PHP`[] ** ++ -- ~ @! * / % + - . = == != === !== & ^&&
PL/I`
Prolog:- ?- ; , . =.. = \= = == \== - + / *spy nospy not is mod
Raku^ &&//`
Smalltalklast1=Goldbergfirst1=Adele
Swift(any Unicode symbol string except) . (including) `! ~ + - * / % =+ =- =* =/ =% &+ &- &* =&+ =&- =&* &&&
Visual Basic .NET() . ! ?() ?. ?! + - * / \ & = ^ = += -= *= /= \= &= ^= =New Await Mod Like Is IsNot Not And AndAlso Or OrElse Xor If(...,...) If(...,...,...) GetXmlNamespace(...) GetType(...) NameOf(...) TypeOf...Is TypeOf...IsNot DirectCast(...,...) TryCast(...,...)
LINQ: From Aggregate...Into Select Distinct Where ...[Ascending Take Skip Let Group...By...Into Join...On
::

References

References

  1. "Operator Input Forms—Wolfram Language Documentation".
  2. "Maxima 5.42.0 Manual: 7. Operators".
  3. "Prefix, Postfix and Circumfix Operators".
  4. "Operators".
  5. (2010). "Developments in Language Theory".
  6. "SWI-Prolog -- op/3".
  7. "Defined Operations". Intel.
  8. "A TUTORIAL INTRODUCTION TO THE LANGUAGE B".
  9. "PHP: Error Control Operators - Manual".
  10. "Operators".
  11. "Functions".
  12. (1983). "Smalltalk-80: The Language and its Implementation". Addison-Wesley.

::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::

operators-(programming)programming-constructs