Expressions
Expressions
html
Part B - Computations
Expressions
Write programs using appropriate types for variables and constants
The C language supports a comprehensive set of operators for transforming existing data into new data. These operators take
one or more operands. The operands may be variables, constants or other expressions. The operators include arithmetic,
relational, and logical operators.
This chapter describes the supported operators in detail, what happens when the operands are of different types, how to change
the type of an operand and the order of evaluation of sub-expressions within expressions. This detailed description begins with
a brief overview of the ALU's and FPA's capabilities. These are the hardware components that evaluate expressions.
HARDWARE
The ALU can only evaluate the simplest of instructions on integer values: for instance, additions where the operands are of the
same type. The same holds for the FPA: it can only evaluate the simplest of instructions on floating-point data. C compilers
simplify expressions into sets of hardware instructions that either the ALU or the FPA can process.
The ALU receives the expression's operator from the CPU's Control Unit, applies that operator to integer values stored in the
CPU's registers and places the result in one of the CPU's registers. The FPA does the same for floating-point values.
The expressions that the ALU can process on integer data are:
arithmetic
relational
logical
The FPA can processes these same kinds of expressions on floating-point data.
ARITHMETIC EXPRESSIONS
Arithmetic expressions on integral operands or floating-point operands are processed by the ALU and the FPA respectively.
1 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
Integral Operands
C supports 5 binary and 2 unary arithmetic operations on integral (int and char) operands. Here, binary refers to two
operands. Unary refers to one operand.
Binary Operations
The binary arithmetic operations on integers are addition, subtraction, multiplication, division and remaindering. Expressions
take one of the forms listed below
Division of one integer by another yields a whole number. If the division is not exact the operation discards the remainder. The
expression evaluates to the truncated integer result; that is, the whole number without any remainder. The expression with the
modulus operator (%) evaluates to the remainder alone.
For example,
Unary Operations
The unary arithmetic operations are identity and negation. Expressions take one of the forms listed below
The plus operator leaves the value unchanged and is present for language symmetry.
Floating-Point Operands
C supports 4 binary and 2 unary arithmetic operations on the floating-point (float and double) operands.
Binary
The binary arithmetic operations on floating-point values are addition, subtraction, multiplication and division. Expressions
take one of the forms listed below
The division operator (/) evaluates to a floating-point result. There is no remainder operator for floating-point operands.
Unary
The unary operations are identity and negation. Expressions take the form listed below
2 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
The plus operator leaves the value unchanged and is present for language symmetry.
Limits
Arithmetic operations can produce values that are outside the range of the expression's type. Consider the following program,
which multiplies two ints and then two doubles
int main(void)
{
int i, j, ij;
double x, y, xy;
ij = i * j;
xy = x * y;
return 0;
}
Compile this program and execute it inputting different values. Try some very small numbers. Try some very large numbers.
When does this program give incorrect results? When it does, explain why?
RELATIONAL EXPRESSIONS
C supports 6 relational operations. A relational expression evaluates a condition. It compares two values and yields 1 if the
condition is true and 0 if the conditon is false. The value of a relational expression is of type int. Relational expressions take
one of the forms listed below
3 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
Example
The following program, accepts two ints and outputs 1 if they are equal; 0 otherwise
// Relational Expressions
// relational.c
int main(void)
{
int i, j, k;
return 0;
}
The first conversion specifier in the format string of the last printf() corresponds to i, the second corresponds to j and the
third corresponds to k.
LOGICAL EXPRESSIONS
C interprets the value 0 as false, interprets any other value as true and supports 3 logical operators. Logical expressions yield 1
if the result is true and 0 if the result is false. The value of a logical expression is of type int. Logical expressions take one of
the forms listed below
Expression Meaning
operand && operand both operands are true
operand || operand one of the operands is true
! operand the operand is not true
Example
The following program, accepts three ints and outputs 1 if the second is greater than or equal to the first and less than or equal
to the third; 0 otherwise:
// Logical Expressions
// logical.c
4 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
int main(void)
{
int i, j, k, m;
return 0;
}
The conversion specifiers in the last printf() correspond to the arguments in the same order (first to j, second to i, etc.).
deMorgan's Law
deMorgan's law is a handy rule for converting conditions in logical expressions. The law states that
The parentheses direct the compiler to evaluate the enclosed expression first.
By applying deMorgan's law, we can often re-write a compound condition in a more readable form.
SHORTHAND ASSIGNMENTS
C also supports shorthand operators that combine arithmetic expressions with an assignment expression.
Integral Operands
C has 5 binary and 2 unary shorthand assignment operators for integral (int and char) operands.
Binary Operands
The binary operators yield the same result as shown in the longhand expressions listed alongside:
5 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
Unary Operands
The unary operators yield the same result as shown in the longhand expressions listed alongside:
We call the unary operator that precedes its operand a prefix operator and the unary operator that succeeds its operand a postfix
operator.
The difference between the prefix and postfix operators is in the value of the expression itself. The prefix operator changes the
value of its operand and sets the expression's value to be the changed value. The postfix operator sets the expression's value to
the operand's original value and then changes the operand's value. In other words, the prefix operator changes the value before
using it, while the postfix operator changes the value after using it.
int main(void)
{
int age = 19;
return 0;
}
Floating-Point Operands
C has 4 binary and 2 unary shorthand assignment operators for floating-point (float and double) operands.
Binary Operands
The binary operators yield the same result as in the longhand expressions listed alongside:
Unary Operands
6 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
The unary operators yield the same result as in the longhand expressions listed alongside:
The prefix and postfix operators operate on floating-point operands in the same way as described above for integral operands.
Ambiguities
Compact use of shorthand operators can yield ambiguous results across different platforms. Consider the following longhand
statements
int i = 5;
int j = i++ + i; // *** AMBIGUOUS ***
One compiler may increment the first i before the addition, while another compiler may increment i after the addition. The C
language does not address this ambiguity and only stipulates that the value must be incremented before the semi-colon. To avoid
ambiguity, we re-write this code to make our intent explicit
int i = 5; int i = 5;
i++; // ++ before int j = i + i; // j is 10
int j = i + i; // j is 12 i++; // ++ after
CASTING
C supports type conversions of any variable, constant or expression. To convert the type of an operand, we precede the operand
with the identifier of the target type enclosed within parentheses. We call such an expression a cast. Casting expressions take
one of the forms listed below
int main(void)
{
int minutes;
float hours;
7 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
printf("Minutes ? ");
scanf("%d", &minutes); Minutes ? 45
hours = (float)minutes / 60;
printf("= %.2lf hours\n", hours); = 0.75 hours
return 0;
}
Note that without the type cast, the output for the same input would be 0.00 hours.
MIXED-TYPE EXPRESSIONS
Since the CPU has separate processors for integral expressions and floating-point expressions (the ALU and the FPA
respectively), C compilers need to convert each mixed-type expression into an expression with operands of the same type.
C compilers use the following promotion ranking to determine the type of the converted operands:
Let us consider assignment expressions separately from arithmetic and relational expressions.
Assignment Expressions
If the left operand in an assignment expression is of a higher type than the right operand, the compiler promotes the right operand
to the type of the left operand. For the example below, the input/output is shown on the right
int main(void)
{
int loonies;
double cash;
printf("Loonies ? ");
scanf("%d", &loonies);
cash = loonies; // promotion Loonies ? 23
printf("Cash is $%.2lf\n", cash); Cash is $23.00
return 0;
}
If the left operand in an assignment expression is of a lower type than the right operand, the compiler truncates the right operand
to the type of the left operand. For the example below, the input/output is shown on the right
8 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
int main(void)
{
double cash;
int loonies;
printf("Cash ? ");
scanf("%lf", &cash);
loonies = cash; // truncation How much cash ? 23.45
printf("%d loonies.\n", loonies); 23 loonies.
return 0;
}
Right Operand
Left long long
double float long int short char
Operand double long
long long long long long long long long long
double double double double double double double double double
long
double double double double double double double double
double
long
float double float float float float float float
double
long long long long long long
long long double float
double long long long long long
long long
long double float long long long long
double long
long long
int double float long int int int
double long
long long
short double float long int short short
double long
long long
char double float long int short char
double long
9 of 10 2/3/2016 8:17 PM
The C Programming Language | ICT - Seneca https://scs.senecac.on.ca/~btp100/pages/content/expre_p.html
For example,
COMPOUND EXPRESSIONS
A compound expression is an expression that contains a sub-expression as one of its operands. C evaluates compound
expressions according to specific rules called rules of precedence. These rules define the order of evaluation of expressions
based on the operators involved. C evaluates the expression with the operator that has the highest precedence first.
The order of precedence, from highest to lowest, and the direction of evaluation are listed in the table below.
To change the order of evaluation, we introduce parentheses. Compilers evaluate the expressions within parentheses (( ))
before applying the rules of precedence. For example,
EXERCISES
Rewrite the expression in the cast.c program listed above so that you obtain the correct result without using a cast
expression
Complete the Workshop on Computations
Complete the following practice problems
Bit Pattern
10 of 10 2/3/2016 8:17 PM