# Operator (computer programming)

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Operator_(computer_programming)
> Markdown URL: https://mediated.wiki/source/Operator_(computer_programming).md
> Source: https://en.wikipedia.org/wiki/Operator_(computer_programming)
> Source revision: 1336679709
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

Basic programming language construct

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Operator" computer programming – news · newspapers · books · scholar · JSTOR (January 2019) (Learn how and when to remove this message)

This article is about operators in computer programming. For other uses, see [Operator (disambiguation)](/source/Operator_(disambiguation)).

In [computer programming](/source/Computer_programming), an **operator** is a [programming language](/source/Programming_language) construct that provides functionality that may not be possible to define as a user-defined [function](/source/Function_(computer_programming)) (e.g. [sizeof](/source/Sizeof) in [C](/source/C_(programming_language))) or has [syntax](/source/Syntax_(programming_languages)) different from that of a function (e.g. [infix](/source/Infix_notation) addition as in a+b). Like other programming language concepts, *operator* has a generally accepted, although debatable, meaning among practitioners.[*[clarification needed](https://en.wikipedia.org/wiki/Wikipedia:Please_clarify)*]

Some operators are represented with symbols – characters typically not allowed for a function [identifier](/source/Identifier_(computer_science)) – 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](/source/Short-circuit_evaluation) 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](/source/Prefix_notation) with fixed [precedence](/source/Order_of_operations) level and associativity and often with compulsory [parentheses](/source/Parentheses) (e.g. Func(a) or (Func a) in [Lisp](/source/Lisp_(programming_language))). 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](/source/Matchfix), [circumfix](/source/Circumfix) or bifix,[1][2][3][4][5] and the syntax of an [expression](/source/Expression_(computer_science)) involving an operator depends on its [arity](/source/Arity) (number of [operands](/source/Operand)), precedence, and (if applicable), [associativity](/source/Operator_associativity). Most programming languages support [binary operators](/source/Binary_operator) and a few [unary operators](/source/Unary_operation), with a few supporting more operands, such as the [?:](/source/%3F%3A) operator in C, which is ternary. There are prefix unary operators, such as unary minus -x, and postfix unary operators, such as [post-increment](/source/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](/source/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](/source/Assignment_(computer_science)) 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](/source/Scope_resolution_operator) and element access operators (as in Foo::Bar and a.b, respectively, in the case of e.g. [C++](/source/C%2B%2B)) 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](/source/Increment_and_decrement_operators) reads the element value of an array and then assigns the element value.

++a[i];

The C++ << operator allows for [fluent](/source/Fluent_interface) syntax by supporting a sequence of operators that affect a single argument. For example:

cout << "Hello" << " " << "world!" << endl;

## ad hoc polymorphic

Further information: [Ad hoc polymorphism](/source/Ad_hoc_polymorphism)

Some languages provide operators that are **ad hoc polymorphic** – inherently overloaded. For example, in [Java](/source/Java_(programming_language)) the + operator sums [numbers](/source/Number) or [concatenates](/source/Concatenate) [strings](/source/String_(computer_science)).

## Customization

Some languages support user-defined [overloading](/source/Operator_overloading) (such as [C++](/source/C%2B%2B) and [Fortran](/source/Fortran)). An operator, defined by the language, can be [overloaded](/source/Function_overloading) to behave differently based on the type of input.

Some languages (e.g. C, C++ and [PHP](/source/PHP)) define a fixed set of operators, while others (e.g. [Prolog](/source/Prolog),[6] [F#](/source/F_Sharp_(programming_language)), [OCaml](/source/OCaml), [Haskell](/source/Haskell)) allow for user-defined operators. Some programming languages restrict operator symbols to special characters like **[+](/source/Addition)** or **[:=](/source/Assignment_(computer_science))** while others allow names like [div](/source/Integer_division#Division_of_integers) (e.g. [Pascal](/source/Pascal_(programming_language))), and even arbitrary names (e.g. [Fortran](/source/Fortran) where an upto 31 character long operator name is enclosed between dots[7]).

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](/source/Lexical_specification) of the language, which affects phrase-level [lexical analysis](/source/Lexical_analysis). Custom operators, particularly via runtime definition, often make correct [static analysis](/source/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](/source/Perl), for example, and some dialects of [Lisp](/source/Lisp_(programming_language)).

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

Further information: [Type conversion](/source/Type_conversion)

Some languages implicitly convert (aka [coerce](/source/Type_conversion#Implicit_type_conversion)) operands to be compatible with each other. For example, [Perl](/source/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](/source/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

See also: [Category:Operators (programming)](https://en.wikipedia.org/wiki/Category:Operators_(programming))

**Mathematical operators**

- [Arithmetic](/source/Arithmetic): such as addition, a + b

- [Relational](/source/Relational_operator): such as [greater than](/source/Greater-than_sign), a > b

- [Logic](/source/Mathematical_logic): such as a AND b or a && b

- [Assignment](/source/Assignment_(computer_science)): such as a = b or a := b

- [Three-way comparison](/source/Three-way_comparison) (aka spaceship): x <=> y

**Program structure operators**

- [Record](/source/Record_(computer_science)) or [object](/source/Object_(computer_science)) [field](/source/Field_(computer_science)) access: such as a.b

- [Scope resolution](/source/Scope_resolution_operator): such as a::b or a.b

**Conditional operators**

- [Ternary conditional](/source/Ternary_conditional_operator): condition ? a : b

- [Elvis](/source/Elvis_operator): x ?: y

- [Null coalesing](/source/Null_coalescing_operator): x ?? y

**Notable C and C++ operators**

- Address-of operator: &x

- [Dereference](/source/Dereference_operator): *p

- [Comma](/source/Comma_operator): e, f

**Compound operators**

- [Compound assignment](/source/Compound_assignment_operator) (aka augmented assignment) in C/C++: +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |=

- [Fused](/source/Fused_operation): such as [cis *x* = cos *x* + *i* sin *x*](/source/Cis_(mathematics))

## Operator features in programming languages

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

Language Symbolic operators Alphanumeric operators Prefix Infix Postfix Precedence Associativity Overloading User-defined overloading User-defined symbols ALGOL 68 each symbolic operator has an alphanumeric equivalent and some a non-ASCII equivalent +* ** * / % %* %× - + &lt; &lt;= >= > = /= & -:= +:= *:= /:= %:= %*:= +=: :=: :/=: non-ASCII: ¬ +× ⊥ ↑ ↓ ⌊ ⌈ × ÷ ÷× ÷* □ ≤ ≥ ≠ ∧ ∨ ×:= ÷:= ÷×:= ÷*:= %×:= :≠: 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 APL + - × ÷ ⌈ ⌊ * ⍟ | ! ○ ~ ∨ ∧ ⍱ ⍲ &lt; ≤ = ≥ > ≠ . @ ≡ ≢ ⍴ , ⍪ ⍳ ↑ ↓ ? ⍒ ⍋ ⍉ ⌽ ⊖ ∊ ⊥ ⊤ ⍎ ⍕ ⌹ ⊂ ⊃ ∪ ∩ ⍷ ⌷ ∘ → ← / ⌿ \ ⍀ ¨ ⍣ & ⍨ ⌶ ⊆ ⊣ ⊢ ⍠ ⍤ ⌸ ⌺ ⍸ (requires ⎕ prefix) Yes (first-order functions only) Yes Yes (higher-order functions only) Higher-order functions precede first-order functions Higher-order functions are left associative, first-order functions are right associative Yes Yes Yes (alphanumeric only) B () [] ! ~ ++ -- + - * & / % << >> < <= > >= == != ^ | ?: = =+ =- =* =/ =% =& =^ =| =<< =>> === =|= =< => =<= =>=[8][9] Yes Yes Yes Yes Yes No No No C () [] -> . ! ~ ++ -- + - * & / % << >> < <= > >= == != ^ | && || ?: = += -= *= /= %= &= ^= |= <<= >>= sizeof Yes Yes Yes Yes Yes Yes No No C++ (same as C) (same as C plus) typeid new delete throw decltype static_cast dynamic cast reinterpret_cast const_cast Yes Yes Yes Yes Yes Yes Yes No C# (same as C plus) ?. ?[] ?? ??= sizeof nameof new stackalloc await throw 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 Yes Yes Yes Yes Yes Yes Yes No Java (same as C) new throw instanceof Yes Yes Yes Yes Yes Yes No No Eiffel [] + - * / // = /= not and or implies "and then" "or else" Yes Yes No Yes Yes No Yes Yes Haskell + - * / ^ ^^ ** == /= > < >= <= && || >>= >> $ $! . ++ !! : (and many more) (function name must be in backticks) Yes Yes No Yes Yes Yes, using Type classes Yes mvBasic Databasic/Unibasic + - * / ^ ** : = ! & [] += -= := # < > <= >= <> >< =< #> => #< AND OR NOT EQ NE LT GT LE GE MATCH ADDS() ANDS() CATS() DIVS() EQS() GES() GTS() IFS() Yes Yes Yes Yes Yes Yes Yes No Pascal * / + - = < > <> <= >= := not div mod and or in Yes Yes No Yes Yes Yes No No Perl -> ++ -- ** ! ~ \ + - . =~ !~ * / % < > <= >= == != <=> ~~ & | ^ && || ' '' // .. ... ?: = += -= *= , => print sort chmod chdir rand and or not xor lt gt le ge eq ne cmp x Yes Yes Yes Yes Yes Yes Yes No PHP [] ** ++ -- ~ @![10] * / % + - . << >> < <= > >= == != === !== <> <=> & ^ | && || ?? ?: = += -= *= **= /= .= %= &= |= ^= <<= >>= clone new unset print echo isset instanceof and or xor Yes Yes Yes Yes Yes No No No PL/I ( ) -> + - * / ** > ¬> >= = ¬= <= < ¬< ¬ & | || Yes Yes No Yes Yes No No No Prolog :- ?- ; , . =.. = \= < =< >= > == \== - + / * spy nospy not is mod Yes Yes Yes Yes Yes No No Yes Raku ++ -- ** ! ~ ~~ * / + - . < > <= >= == != <=> & | ^ && || // [11] print sort chmod chdir rand and or not xor lt gt le ge eq ne leg cmp x xx Yes Yes Yes Yes Yes Yes Yes Yes[12] Rust () [] . ! + - * & ? / % << >> < <= > >= == != ^ | && || .. ..= = += -= *= /= %= &= ^= |= <<= >>=[13] &mut&raw const&raw mutas[13] Yes Yes Yes Yes Yes Yes Yes No Smalltalk (up to two characters[14]) (alphanumeric symbols need a colon suffix) No Yes Yes No No Yes Yes Yes Swift (any Unicode symbol string except) . (including) ! ~ + - * / % =+ =- =* =/ =% &+ &- &* =&+ =&- =&* && || << >> & | ^ == != < <= > >= ?? ... ..< is as as? Yes Yes Yes Yes (defined as partial order in precedence groups) Yes (defined as part of precedence groups) Yes Yes Yes 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 <Order By>...[Ascending|Descending] Take <Take While> Skip <Skip While> Let Group...By...Into Join...On <Group Join...On...Into> Yes Yes Yes Yes Yes Yes Yes No Zig () [] {} . ! ~ + +% +| - -% -| * *% *| & / % << <<| >> < <= > >= == != ^ | ++ ** .* .? = += +%= +|= -= -%= -|= *= *%= *|= /= %= &= ^= |= <<= <<|= >>=[15] and or orelse catch[15] Yes Yes Yes Yes Yes Yes No No

## See also

- [Operators in C and C++](/source/Operators_in_C_and_C%2B%2B)

## References

1. **[^](#cite_ref-1)** ["Operator Input Forms—Wolfram Language Documentation"](https://reference.wolfram.com/language/tutorial/OperatorInputForms.html.en). *reference.wolfram.com*.

1. **[^](#cite_ref-2)** ["Maxima 5.42.0 Manual: 7. Operators"](https://maxima.sourceforge.net/docs/manual/maxima_7.html). *maxima.sourceforge.net*.

1. **[^](#cite_ref-3)** ["Prefix, Postfix and Circumfix Operators"](https://mythryl.org/my-Prefix__Postfix_and_Circumfix_Operators.html). *mythryl.org*.

1. **[^](#cite_ref-4)** ["Operators"](http://doc.perl6.org/language/operators#___top). *doc.perl6.org*.

1. **[^](#cite_ref-Pribavkina_5-0)** Pribavkina, Elena V.; Rodaro, Emanuele (2010). "State Complexity of Prefix, Suffix, Bifix and Infix Operators on Regular Languages". *Developments in Language Theory*. Lecture Notes in Computer Science. Vol. 6224. pp. 376–386. [doi](/source/Doi_(identifier)):[10.1007/978-3-642-14455-4_34](https://doi.org/10.1007%2F978-3-642-14455-4_34). [ISBN](/source/ISBN_(identifier)) [978-3-642-14454-7](https://en.wikipedia.org/wiki/Special:BookSources/978-3-642-14454-7).

1. **[^](#cite_ref-6)** ["SWI-Prolog -- op/3"](https://www.swi-prolog.org/pldoc/man?predicate=op/3). *www.swi-prolog.org*.

1. **[^](#cite_ref-IntelFortran_7-0)** ["Defined Operations"](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-0/defined-operations.html). Intel. Retrieved 6 May 2025.

1. **[^](#cite_ref-8)** ["A TUTORIAL INTRODUCTION TO THE LANGUAGE B"](https://web.archive.org/web/20170403063756/https://www.bell-labs.com/usr/dmr/www/btut.html). Archived from [the original](https://www.bell-labs.com/usr/dmr/www/btut.html) on 2017-04-03. Retrieved 2024-08-03.

1. **[^](#cite_ref-9)** ["Thompson's B Manual"](https://web.archive.org/web/20240917142452/https://www.bell-labs.com/usr/dmr/www/kbman.html). *www.bell-labs.com*. Archived from [the original](https://www.bell-labs.com/usr/dmr/www/kbman.html) on 2024-09-17. Retrieved 2026-02-04.

1. **[^](#cite_ref-10)** ["PHP: Error Control Operators - Manual"](https://php.net/manual/en/language.operators.errorcontrol.php). *php.net*.

1. **[^](#cite_ref-11)** ["Operators"](https://docs.perl6.org/language/operators). *docs.perl6.org*.

1. **[^](#cite_ref-12)** ["Functions"](https://docs.perl6.org/language/functions#Defining_Operators). *docs.perl6.org*.

1. ^ [***a***](#cite_ref-:0_13-0) [***b***](#cite_ref-:0_13-1) ["Operator expressions - The Rust Reference"](https://doc.rust-lang.org/reference/expressions/operator-expr.html). *doc.rust-lang.org*. Retrieved 2026-02-04.

1. **[^](#cite_ref-BinaryMessages_14-0)** Goldberg, Adele; Robson, David (1983). [*Smalltalk-80: The Language and its Implementation*](http://stephane.ducasse.free.fr/FreeBooks/BlueBook/Bluebook.pdf) (PDF). Reading, Mass.: Addison-Wesley. [ISBN](/source/ISBN_(identifier)) [0-201-11371-6](https://en.wikipedia.org/wiki/Special:BookSources/0-201-11371-6).

1. ^ [***a***](#cite_ref-:1_15-0) [***b***](#cite_ref-:1_15-1) ["Documentation - The Zig Programming Language"](https://ziglang.org/documentation/master/#Operators). *ziglang.org*. Retrieved 2026-02-05.

---
Adapted from the Wikipedia article [Operator (computer programming)](https://en.wikipedia.org/wiki/Operator_(computer_programming)) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Operator_(computer_programming)?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
