This chapter presents some puzzles involving arithmetic expressions. The puzzles are easy, but several of them are based on mistakes found in student programs (and midterms!) For a detailed discussion of expressions, see your textbook.
An expression in a C program is a combination of one or more explicit values, constants, variables, operators, parentheses, and functions that produce a value when the program runs. The syntax rules of C determine if an expression is valid.
At run time, an expression is evaluated when control reaches a statement containing the expression. An arithmetic expression is one that (at run time) produces a numerical result. Precedence and associativity of operators determine how the expression is evaluated. The following are arithmetic expressions.
45 a single explicit value is an expression grandTotal a single variable is an expression grandTotal+45 a combination of a variable, an operator, and an explicit value (grandTotal+45)/2 syntactically correct expression (grandTotal+45)/2 + myFun(a+b) syntactically correct expression
An explicit value is often called a literal.
The expression 45
is an int
literal.