liveBook · Manning

Operator

As explained in Modern C, Third Edition

An operator is a C symbol or symbol sequence that specifies an operation on one or more operands.

An operator is a C symbol or symbol sequence that specifies an operation on one or more operands. In a + b, + is the operator, while a and b are operands. section 4.1

Binding and arithmetic

With size_t a = 45 and size_t b = 7, (a - b) * 2 produces 76, whereas a - b * 2 produces 31. Parentheses group sub-expressions and enforce the intended binding. Unary + provides a value and unary - produces its negative. section 4.2.1

C organizes operators by what they operate on:

  • Value operators perform arithmetic, bit, comparison, and related operations. section 4.1
  • Object operators evaluate, assign, increment, access members, subscript arrays, obtain addresses, and determine size or alignment. section 4.1
  • Type operators such as sizeof, alignof, and offsetof produce integer constants describing size, alignment, or member offset. section 4.1

Operator meaning depends on operand types. Binary expressions such as a*b+c can work across wide integer, floating, complex, and pointer types; conversions reconcile operands when required, while some operators accept differing types directly. section 18.1.1

For size_t, +, -, and * produce the mathematically correct result when representable; unsigned arithmetic remains well defined, but an unrepresentable result is arithmetic overflow. section 4.2.1

Operators also participate in sequencing. An operator runs after its operands are evaluated, and assignment, increment, or decrement updates occur after operand evaluation; side effects can still produce indeterminate results in larger expressions. section 19.2

Table 4.1 Value operators: The Form column gives the syntactic form of the operation, where @ represents the operator and a and possibly b denote values that serve as operands. For arithmetic and bit operations, the type of the result is a type that reconciles the types of a and b. For some of the operators, the Nick column gives an alternative form of the operator or lists a combination of operators that has special meaning. Most of the operators and terms will be discussed later.
Type restriction
Operator Nick Form a b Result
a Narrow Wide Promotion
+ - a@b Pointer Integer Pointer Arithmetic
+ - * / a@b Arithmetic Arithmetic Arithmetic Arithmetic
+ - @a Arithmetic Arithmetic Arithmetic
% a@b Integer Integer Integer Arithmetic
  compl @a Integer Integer Bit
& bitand a@b Integer Integer Integer Bit
| bitor
^ xor
<< >> a@b Integer Positive Integer Bit
== < > <= >= a@b Scalar Scalar 0,1 Comparison
!= not_eq a@b Scalar Scalar 0,1 Comparison
!!a a Scalar 0,1 Logic
!a not @a Scalar 0,1 Logic
&& || and or a@b Scalar Scalar 0,1 Logic
. a@m struct Value Member
* @a Pointer Object Reference
[] a[b] Pointer Integer Object Member
-> a@m struct pointer Object Member
() a(b ...) Function pointer Value Call
sizeof @ a None size_t Size, ICE
alignof _Alignof @(a) None size_t Alignment, ICE

Use the value-operator table as a compact reference for forms, operand restrictions, promotions, and result types. section 4.1

How Modern C, Third Edition covers it

How deep it goes
Defines operands, catalogs value, object, and type operators, then develops arithmetic, conversions, and sequencing constraints.
Examples
Uses a + b, size_t arithmetic with parentheses, operator tables, and function calls whose side effects can vary in order.
What you can do after
Evaluate/understand operator expressions by identifying operands, grouping sub-expressions, checking types, and recognizing sequencing constraints.
How it connects
Connects operators with operands, expressions, types, conversions, arithmetic overflow, and sequencing.

Also explained in

Compare how 7 books explain operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.