An expression is a combination of operators and operands.
Unary operators require only 1 operand (e.g. line 6 in Exercise 2).
Binary operators require 2 operands (e.g. line 8 in Exercise 2).
There is only one ternary operator in the C language, which is the conditional operator ? : that requires 3 operands (e.g. line 10 in Exercise 2).
Arithmetic operators include:
Operator
Example
Meaning
*
x * y
Multiply x and y
/
x / y
Divide x by y
%
x % y
The remainder after dividing x by y
+
x + y
Add x and y
-
x - y
Subtract y from x
+
+x
Value of x
-
-x
Arithmetic negative of x
++
++x
Increase x by 1 before using x
--
--x
Decrease x by 1 before using x
++
x++
Increase x by 1 after using x
--
x--
Decrease x by 1 after using x
Exercise 3: Arithmetic operators
Write C programs that display multiples of 5 from zero to 50, i.e. the multiplication table for 5. Find different ways using all the arithmetic operators in the above section.
What are the evaluation trees for the arithmetic expressions?
Operators in C arithmetic expressions follow the same order of precedence as normal math expressions.
Parentheses are the highest precedence
Unary operators, such as ++ or --
Positive and negative sign operators, e.g. + or -
Multiplication and division operators
Addition and subtraction operators
Assignment operators are the lowest precedence, such as = or *=
Like math expressions, parentheses can be used to indicate specific orders of precedence. The use of extra parentheses in a complex arithmetic expression can make it easier to read and change later.
Exercise 5: Limitations of output display
Type the following source code into the C compiler.
What is the data type at each statement? How does the data type restrict the output display?
The result of division using all integer operands is an integer value. If a fractional result is required, then one of the operands must be forced to be float or double type.
Assignment operators include:
Operator
Example
Meaning
=
x = y
Assign the value of y to the variable x
*=
x *= y
Assign the product of x multiplied by y to the variable x
/=
x /= y
Assign the quotient of x divided by y to the variable x
+=
x += y
Assign the sum of x and y to the variable x
-=
x -= y
Assign the result of subtracting y from x to the variable x
Exercise 6: Assignment operators
Type the following source code into the C compiler.
#include <stdio.h>
int main(void) { int num1, num2;
num1 = 5;
num2 = num1 + 10; printf("num1 is %d and num2 is %d \n", num1, num2 );
num1 = 50; printf("num1 is %d and num2 is %d \n", num1, num2 );
num1 += 34 / 17;
num2 *= 34 - 17; printf("num1 is %d and num2 is %d \n", num1, num2 ); }
When using the combination assignment operators (e.g. *=), the expression on the right side of the operator is fully evaluated before the assignment operator is applied.
x *= 37 - 15;
is the same as
x *= (37 - 15);
Comparison operators have only 2 possible results:
Value 1 to mean logical true.
Value 0 to mean logical false.
Operator
Example
Meaning
<
x < y
1 if x is less than y
<=
x <= y
1 if x is less than or equal to y
>
x > y
1 if x is greater than y
>=
x >= y
1 if x is greater than or equal to y
==
x == y
1 if x is equal to y
!=
x != y
1 if x is not equal to y
Exercise 7: Comparison operators
Type the following source code into the C compiler.
int main(void) { double furlong = 50; double miles = furlong * 0.125; double kilometers = furlong * 0.2011680;
printf("%.0lf furlongs is equivalent to %.2lf miles \n", furlong, miles ); printf("%.0lf furlongs is equivalent to %.2lf kilometers", furlong, kilometers ); }
Exercise 10: Temperature conversion
Write a C program to display the following calculations:
28 Kelvin into its equivalent in Celsius
324 Kelvin into its equivalent in Fahrenheit
The conversion formulas are available at Wikipedia: