02-Expressions in C
02-Expressions in C
Expressions
EXPRESSIONS in C
1. Summary
This paper presents the notion of an expression in the C/C++ programming language. It also
presents the operators and the way an expression is evaluated.
2.2. Operators
- Arithmetic operators:
- Unary operators: +,
- Binary multiplicative operators: *, /, %
- Binary additive operators: +,
- Relational operators: <, <=, >, >=
- Equality operators: = =, !=
- Logic operators: !, &&, ||
- Logic operators on bits: ~, <<, >>, &, ^, |
- Assignment operators: =, /=, *=, %=, +=, =, <<=, >>=, &=, ^=, |=
- Pre- and post increment operators : ++,
- Type cast operators: (type)
- Resolution operator: ::
- An operator for the reference type: &
- An operator to allocate/deal locate the heap:
- Type constructor/destructor invocation operators: new/delete
Operator precedence, in the decreasing order of their priority, is given in the table below:
Priority Operators
15-maximum ( ) [ ] ->
14 +(unary) -(unary) &(unary) *(unary) ++ --
(type) sizeof ! ~
13 *(binary) / %
12 +(binary) -(binary)
11 << >>
10 < <= > >=
9 = = !=
8 &(binary)
7 ^
6 |
5 &&
4 ||
3 ? :
2 = <<= >>= += -= *= /= %= &= ^= |=
1-minimm ,
Operators are associated in the left-to-right order, except for the unary, conditional, and assignment
operators which associate right-to-left.
3. Assignments
3.1. Write a program to calculate the value z= x**y, for variables x and y of the type double.
3.2. Explain the difference between real (floating point) division and the integer division.
3.3. Write a program that reads the value of an angle expressed in degrees and calculates the values for
its sine, cosine and tangent.
3.4. Write a program that reads a positive integer number in the range [1600, 4900]. Knowing that that
number represents a year, check whether that year is bissextile or not.
3.5. Using conditional expressions, write a program which reads a real value for x and then computes the
value for the function:
x 2 7 x + 4 if x < 2
f ( x) = 0 if x = 2
x 2 + 5x 2 if x > 2
3.6. Write a program which reads a real number x, representing a measurement for an angle in Radians,
and then converts it to degrees, minutes, and seconds.
3.7. Write a program to simulate the operation of a counting clock (indicating the hour, minute, and
second).
3.8. Write a program to show the number of bytes the C/C++ primitive data types take in the computer
memory.
3.9. Convert to binary, by computation, your birth year and the current year.
a) Show how the two years are represented as data of type int.
b) Show the effect the shift operations: left shift by 4 bits, right shift by 2 bits, and 1s
complement applied to this data.
c) Show the effect of the bitwise operations &, ^, | , upon the provided data.
d) Write a program to check the correctness your calculations.
3.10. Write a program which effects arithmetic operations upon two data items, one of which is an
integer, and the other a real number. Execute this program using values which yield results outside the
internal representation limits. What happens when limits are over(under)flowed?
3.11. Write a program which reads the integer numbers a, b, c, d and outputs the highest value of
fractions a/b and c/d.
3.12. Write a program to determine the relative position of a straight line in relationship with a given
circle. Your program will read: the coordinates of the center of the circle, its radius, and the coordinates
for two points located on the straight line.
3.13. Write a program which reads the planar coordinates of the vertices of a triangle, and then describes
the relative position (i.e. above, below, left, right) of this triangle in relationship with a point in the same
plane, given by its coordinates.
3.14. Write a program to calculate the ideal weight of a person using the following formulas:
Sex, height (in centimeters), and age (in years) are to be read.
3.15. Write a program to convert Cartesian coordinates of a given point to polar coordinates.
Hints. The two polar coordinates r and can be converted to the Cartesian coordinates x and y by using
the trigonometric functions sine and cosine:
while the two Cartesian coordinates x and y can be converted to polar coordinate r by
One may avoid having to keep track of the numerator and denominator signs by use of the atan2
function, which has separate arguments for the numerator and the denominator.