ArticleslgStudy

computer science

Operator (computer programming)

Operator (computer programming) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Operator (computer programming) rather than just read about it. In short: 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.

Key takeaways

  • Operator (computer programming) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Operator (computer programming) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Operator (computer programming) from memory before moving on to harder problems.

Reference excerpt

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.

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

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 (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 Arithmetic: such as addition, a + b Relational: such as greater than, a > b Logic: such as a AND b or a && b Assignment: such as a = b or a := b Three-way comparison (aka spaceship): x <=> y Program structure operators Record or object field access: such as a.b Scope resolution: such as a::b or a.b Conditional operators Ternary conditional: condition ? a : b Elvis: x ?: y Null coalesing: x ?? y Notable C and C++ operators Address-of operator: &x Dereference: *p Comma: e, f

Compound operators Compound assignment (aka augmented assignment) in C/C++: +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, |= Fused: such as cis x = cos x + i sin x

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

See also Operators in C and C++

References

Worked examples

Example 1 — a first encounter with Operator (computer programming)

Start with the simplest possible case. Write down what Operator (computer programming) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Operator (computer programming) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Operator (computer programming) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Operator (computer programming)

In research
Operator (computer programming) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Operator (computer programming) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Operator (computer programming) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Operators (programming), Programming constructs, so understanding it makes those chapters shorter.
In everyday life
Look for Operator (computer programming) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Operator (computer programming)” →

How to study Operator (computer programming) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Operator (computer programming) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Operator (computer programming) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Operator (computer programming) in simple terms?

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 conc…

Why does Operator (computer programming) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Operator (computer programming)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Operator (computer programming).

Tags

  • Operators (programming)
  • Programming constructs

Keep exploring