liveBook · Manning

Expression

As explained in Modern C

An expression is a C syntactic construct that is evaluated to produce a value and may modify program state.

Consider: double x = 5.0; double y = 3.0; x = (x * 1.5) - y; printf("x is %g\n", x); The multiplication and subtraction produce intermediate values; the assignment changes x; printf observes the resulting value. An expression is a C syntactic construct evaluated to produce a value, and some expressions also modify program state. Operators such as +, -, ==, and != act on operands; expressions may involve values, objects, or types. section 5.1 section 4.0

TAKEAWAY: C expressions combine several related operations.

  • Value expressions perform arithmetic, comparisons, boolean work, conditional selection, and bit operations. section 4.0
  • Object expressions access, assign, address, index, or modify objects. Assignment operators return the modified value, while ++ and -- differ between prefix and postfix forms. section 4.0 section 4.2
  • Type expressions such as sizeof, _Alignof, and offsetof produce integer constants of type size_t. section 4.0
  • Side effects make evaluation harder to follow: avoid modifying more than one object in a statement. section 4.2

Expressions as state transitions

Starting with x = 5.0 and y = 3.0, x = (x * 1.5) - y evaluates x to 5.0, computes 7.5, computes 4.5, and stores 4.5 in x. A compiler may replace unobservable intermediate work with equivalent code, provided the observable result remains unchanged. section 5.1

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. (view table figure)
     

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

Browse the value-operator table to compare syntactic forms, operand restrictions, promotions, and result categories. section 4.0

How Modern C covers it

How deep it goes
Defines value, object, and type operators with examples, then examines side effects and optimization without exhausting every operator rule.
Examples
Uses arithmetic assignment, prefix and postfix increments, compound assignments, printf observation, and scalar or aggregate initializers.
What it assumes
Assumes basic C variables and values while establishing operators, operands, size_t, unsigned integers, and expression evaluation.
What you can do after
Evaluate expressions as value and state transitions, and recognize when side effects or optimization affect their behavior.
How it connects
Connects expressions to operators, operands, objects, types, side effects, initialization, optimization, and the abstract machine.

Also explained in

Compare how 11 books explain expression →

Related concepts

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