Lably
Skip to content

Operator (computer programming)

Basic programming language construct

  • 5 min read
  • 6 chapters
  • 1 lab on Lably

Learn it on Lably

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.

<span id="mwZQ"></span><span class="o" id="mwZg">++</span><span class="n" id="mwZw">a</span><span class="p" id="mwaA">[</span><span class="n" id="mwaQ">i</span><span class="p" id="mwag">];</span>

The C++ << operator allows for fluent syntax by supporting a sequence of operators that affect a single argument. For example:

<span id="mwcg"></span><span class="n" id="mwcw">cout</span><span class="w" id="mwdA"> </span><span class="o" id="mwdQ"><<</span><span class="w" id="mwdg"> </span><span class="s" id="mwdw">"Hello"</span><span class="w" id="mweA"> </span><span class="o" id="mweQ"><<</span><span class="w" id="mweg"> </span><span class="s" id="mwew">" "</span><span class="w" id="mwfA"> </span><span class="o" id="mwfQ"><<</span><span class="w" id="mwfg"> </span><span class="s" id="mwfw">"world!"</span><span class="w" id="mwgA"> </span><span class="o" id="mwgQ"><<</span><span class="w" id="mwgg"> </span><span class="n" id="mwgw">endl</span><span class="p" id="mwhA">;</span>

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication