Operators and Expressions
Operators and Expressions
EXPRESSIONS
Arithmetic Operators
OperatorMeaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% Modulo division
Example:
ab
a+b
a*b
a/ba%b
-a * b
Integer division truncates any fractional part
Modulo division produces remainder of an integer
division
The unary minus operator, multiplies its single
operand by -1
Integer Arithmetic
When both the operands in a single
Real Arithmetic
An arithmetic operation involving
Mixed-mode Arithmetic
When one of the operands is real and
Relational Operators
We compare two quantities and depending on their
Logical Operators
C has three logical operators
&& meaning logical AND
|| meaning logical OR
! meaning logical NOT
The logical operators && and || are used when we
Assignment Operator
Assignment operators are used to assign the
Assignment Operator
Statement with simple
Statement with
assignment operator shorthand operator
a=a+1
a += 1
a=a-1
a -= 1
a = a * (n+1)
a *= n+1
a = a / (n+1)
a /= n+1
a=a%b
a %= b
The use of shorthand assignment operators has
three advantages:
What appears on the left-hand side need not be repeated and
Conditional Operators
A ternary operator pair ?: is available to
Bitwise Operators
Bitwise operators are used for manipulation of data
at bit level
These operators are used for testing the bits, or
shifting them right or left
This operators may not be applied to float or double
The >> (Right shift) and << (left shift) operators
move the bits the number of bit positions specified.
Bitwise Operators
Bitwise Operators
Unary Operator (~)/One's Complement
The one's complement (~) or the bitwise
complement gets us the complement of a
given number.
The bits gets inverted, for every bit 1 the result
is bit 0 and conversely for every bit 0 we have
a bit 1.
Special Operators
Some special operator are
Comma operator
sizeof operator
Pointer operators (& and *)
Member selection operator (. And )
Comma Operator
Used to link the related expressions
together
A comma-linked list of expressions
are evaluated left to right and the
value of right-most expression is the
value of the combined expression
Example:
Value = (x = 10, y = 5, x+y);
sizeof Operator
The sizeof is a compile time operator
When used with an operand, it returns the
Arithmetic Expressions
An arithmetic expressions is a
Evaluation of Expressions
Expressions are evaluated using a
assignment statements
variable = expression;
The expression is evaluated first
The result then replaces the previous value
of the variables on the left-hand side
Example:
x = a *b c;
y = b / c * a;
z = a b / c + d;
Precedence of Arithmetic
Operators
An arithmetic expressions without
PROGRAM
main()
{
float a, b, c, x, y, z;
a = 9;
b = 12;
c = 3;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a - (b / (3 + c) * 2) - 1;
printf("X = %f\n",x);
printf("Y = %f\n",y);
printf("Z = %f\n",z);
}
OUTPUT:
X = 10.000000
Y = 7.000000
Z = 4.000000
Program Explanation
x=a-b/3+c*2-1
x = a - b/3 + c*2 - 1
when a=9, b=12 and c=3
x = 9 - 12/3 + c*2 -1
First pass:
Step1: x = 9-4+3*2-1
Step2: x = 9-4+6-1
Second pass:
Step3: x = 5+6-1
Step4: x = 11-1
Step5: x = 10
The order of expressions can be changed by introducing
Some computational
problem
Division by zero
Any attempt to divide a number by zero
Type Conversions in
Expressions
Automatic Type Conversion
part
double to float causes rounding of digits
long int to int causes dropping of the excess
higher order bits
Casting a Value
There are instances when we want to force
associated with it
This precedence is used to determine how
an expressions involving more than one
operator is evaluated
There are distinct levels of precedence
The operator at higher level of precedence
are evaluated first
The operators of same precedence are
evaluated either from left to right or from
right to left, this is known as associativity
levels and their rules of association
Examples
if (x == 10 + 15 && y < 10)
The precedence rule says that the addition operator has a
higher priority than the logical operator (&&) and the
relational operator ( == and < ).
The addition of 10 and 15 is executed first
if (x == 25 && y < 10)
the next step is to determine x is equal to 25 and y is less
than 10.
If we assume a value of 20 for x and 5 for y, then
x == 25 is FALSE (0)
y < 10 is TRUE (1)
Since operator < have higher priority compared to ==, y < 10
is tested first and then x == 25 is tested
if (FALSE && TRUE)
Because on of the condition is false, the complex condition is
FALSE
In the case of &&, it is guaranteed that the second operand
will not be evaluated if the first is zero
In the case of ||, the second operand will not be evaluated if
the first is non-zero
Mathematical Functions
Many mathematical functions are
THE END