C Expression
C Expression
Expressions
Introduction
1. Arithmetic (+, -, *, /, %)
2. Relational (<, <=, >, >=, ==, !=)
3. Logical (&&, ||, !)
4. Assignment (=, +=, -=, *=, /=, %=)
5. Increment(decrement) (++, --)
6. Conditional (? :)
7. Bitwise (&, |, ^, <<, >>)
8. Special (,,sizeof)
Arithmetic operators
C provides all basic arithmetic operators
The operators that are used to perform arithmetic operation such as addition,
subtraction, multiplication, division etc are called arithmetic operators.
The operators are +, - ,* , / , %.
Let a=10, b=5. Here, a and b are variables and are known as operands.
Operator Meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division (applied only to integers)
Relational operators
We often compare two quantities and depending on their relation, take certain
decisions.
Operator Meaning
< Is less than
<= Is less than or equal to
> Is greater than
>= Is greater than or equal
to
== Is equal to
!= Is not equal to
We can simplify an expression involving the not and the less than operator
using the complements as shown below:
Actual one Simplified one
!(x<y) x => y
!(x>y) x <= y
!(x!=y) x == y
!(x<=y) x>y
!(x>=y) x<y
!(x==y) x != y
Logical operators
I an addition to the relational operators, C has the following three logical operators.
|| Meaning logical OR
! Meaning logical NOT
• They are used when we need to test more than one conditions to
make decisions.
• An example is:
a > b && x == 10
The compound expression is true when a > b and x == 10 is true.
Logical operators
Non-zero Non-zero 1 1
Non-zero 0 0 1
0 Non-zero 0 1
0
Relative precedence of the0 relational and
0 logical operators
0 is as
follows
Highest priority !
> >= < <=
== !=
&&
Lowest priority ||
Assignment operators
We have already applied the usual assignment operator. In addition, C has a set of
shorthand assignment operators of the form
v op= expr;
is equivalent to
v = v op (expr)
C allows two very useful operators not generally found in other languages.
There are the increment and decrement operators:
++ and --
The operator ++ adds 1 to the operand, while -- subtracts 1.
(1) (2)
4
(3) 6
5
(4)
11
(5
)
10
Plot similar diagram for 9 - 12/(3 + 3)*(2-1). Consider that ( ) has the highest
priority!
Some computational problems
Computers give approximate values for real numbers and the error due to such
approximations may lead to serious problems:
a = 1.0/3.0;
b = a * 3.0;
We know that (1.0/3.0) * 3.0 is equal to 1. But there is not guarantee that the value of b
computed in a program will equal 1.
Division by zero. Any attempt to divide a number by zero will result in abnormal
termination of the program. (We should avoid it)
Overflow and underflow errors. It is our responsibility to guarantee that
operands are of the correct type and range.
Type conversions in expressions
long float
float
int double
Type conversions in expressions
long double
double
Conversion
hierarchy float
unsigned long
int
long int
unsigned int
int
short char
Type conversions in expressions
The operator (float) converts the females_number to floating point for the purpose of
evaluation of the expression.
The general form of a cast (explicit conversion) is
(type-name)expression
Type conversions in expressions
Example Action
x = (int) 7.5 7.5 is converted to integer by truncation.
a = (int) 21.3 / (int) Evaluated as 21/4 and result would be 5.
4.5
b = (double) sum/n Division is done in floating mode.
y = (int) (a + b) The result of a + b is converted to integer
z = (int) a + b a is converted to integer and the added to b
p = cos((double) x) Converts x to double before using it.
Operator precedence and associativity