100% found this document useful (1 vote)
246 views

Operators and Expressions

Operators are symbols that tell the computer to perform mathematical or logical manipulations on data. C operators can be classified into arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise categories. Expressions combine variables, constants, and operators according to C's precedence rules. Care must be taken to avoid errors from division by zero, overflow, underflow, and approximation in real number expressions.

Uploaded by

Siddharth Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
246 views

Operators and Expressions

Operators are symbols that tell the computer to perform mathematical or logical manipulations on data. C operators can be classified into arithmetic, relational, logical, assignment, increment/decrement, conditional, and bitwise categories. Expressions combine variables, constants, and operators according to C's precedence rules. Care must be taken to avoid errors from division by zero, overflow, underflow, and approximation in real number expressions.

Uploaded by

Siddharth Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 37

OPERATORS AND

EXPRESSIONS

OPERATORS AND EXPRESSIONS


INTRODUCTION
An operator is a symbol that tells the computer to

perform certain mathematical or logical


manipulations
Operators are used in programs to manipulate data
and variables
C operators can be classified into a number of
categories. They include
1. Arithmetic operators.
2. Relational operators.
3. Logical operators.
4. Assignment operators.
5. Increment and decrement operators.
6. Conditional operators.
7. Bitwise operators.
8. Special operators.

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

arithmetic expression such as a + b are


integers, the expression is called an integer
expression
The operation is called integer arithmetic
If a and b are integers and a=14 & b=4 then
a b =10
a + b = 18
a * b = 56
a / b = 3 (decimal part truncated)
a % b = 2 (remainder of division)

Real Arithmetic
An arithmetic operation involving

only real operands is called real


arithmetic
If x, y and z are floats, we have
x = 6.0/7.0 = 0.857143
y = 1.0/3.0 = 0.333333
z = -2.0/3.0 = -0.666667
The operator % cannot be used with
real operands

Mixed-mode Arithmetic
When one of the operands is real and

the other is integer, the expression is


called a mixed mode arithmetic
expression
If either operand is of real type, then
only the real operation is performed
and the result is always a real number
15/10.0 = 1.5
15/10 = 1

Relational Operators
We compare two quantities and depending on their

relation, take certain decisions


These comparisons can be done with the help of relational
operators
An expression a < b or 1 < 20 containing a relational
operator is termed as a relational expression
The value of relational expression is either zero or one
One for true and zero for false
Relational expressions are used in decision statements
such as if and while

Logical Operators
C has three logical operators
&& meaning logical AND
|| meaning logical OR
! meaning logical NOT
The logical operators && and || are used when we

want to test more than one condition and make


decision
Example:
a > b && x == 10
An expression of this kind which combines two or
more relational expressions is termed as a logical
or compound relational expressions
The logical expressions also yield a value of one
or zero
Example:
If (age > 55 && salary < 1000)
If (number < 0 || number > 100)

Assignment Operator
Assignment operators are used to assign the

result of an expression to a variable


The usual assignment operator: =
C has a set of shorthand assignment
operators of the form
v op= exp;
Where v is a variable, exp is an expression
and op is a C binary arithmetic operator
op= is known as the shorthand assignment
operator
The assignment statement
v op= exp; is equivalent to v = v op (exp);
x +=3; is equivalent to x = x + 3;

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

therefore it becomes easier to write


The statement is more concise and easier to read
The statement is more efficient

Increment and Decrement


Operator
Increment and decrement operators are:

++ and -The operator ++ adds 1 to the operand


while subtracts 1
Both are unary operator and take the form:
++m; or m++; is equivalent to m =
m+1; or m+=1;
--m; or m--; is equivalent to m = m-1; or
m-=1;
Used in for and while loops
While ++m and m++ mean the same
thing when they form statements
independently

Increment and Decrement


Operator

They behave differently when they are used

in expressions on the right-hand side of an


assignment statement
m=5;
y=++m;
The value of y and m would be 6
m=5;
y=m++;
The value of y would be 5 and m would be 6
A prefix operator first adds 1 to the operand and
then the result is assigned to the variable on left
A postfix operator first assigns the value to the
variable on left and then increments the operand

Conditional Operators
A ternary operator pair ?: is available to

construct conditional expressions of the form


condition ? expression1 : expression2
First condition is evaluated, if it is a nonzero
(true), then the expression1 is evaluated and
becomes the value of the expression
If it is zero (false), then expression2 is
evaluated
a = 10;
b = 15;
x = (a > b) ? a : b;

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

number of bytes the operand occupies


The operand may be a variables, a constants or
a data type qualifier
Example:
m = sizeof(sum);
n = sizeof(long int);
k = sizeof(235L);
Normally used to determine the lengths of
arrays and structures when their sizes are not
known to the programmer
Also used to allocate memory space dynamically
to variables during the execution of the program

Arithmetic Expressions
An arithmetic expressions is a

combination of variables, constants,


and operators arranges as per the
syntax
C can handle any complex
mathematical expressions

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

parentheses will be evaluated from left to


right using the rules of precedence of
operators
There are two distinct priority level
High priority * / %
Low priority
+Evaluation procedure includes two left-toright passes
First pass: the high priority operators are
applied
Second pass: the low priority operators
are applied

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

parenthesis into an expression


Parenthesis may be nested. In such cases, evaluation of
expression will proceed outward from the innermost set of
parentheses

Some computational
problem

When expressions include real

values, it is important to take


necessary to guard against certain
computational errors
Some computational errors are
Errors due to approximation
Division by zero
Overflow and underflow

Errors due to approximation


Computers gives approximate values

of real numbers and the errors due to


approximation may lead to serious
problems
a = 1.0/3.0
b = a * 3.0
The value is equal to 1 but the value
of b computed in a program will not be
1

Division by zero
Any attempt to divide a number by zero

will result in abnormal termination of the


program
In some cases division may produce
meaningless results

Overflow and underflow


It is our responsibility to guarantee
that operands are of the correct type
and range and the results may not
produce any overflow and underflow

Type Conversions in
Expressions
Automatic Type Conversion

C permits mixing of constants and variables of

different types in an expression


If the operands are of different types, the lower
type is automatically converted to the higher
type before the operation proceeds. The result is
of higher type
The final result of an expression is converted to
the type of the variable on the left of the
assignment sign before assigning the value to it
The following changes are introduced during the
final assignment
float to int causes truncation of the fractional

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

conversion in a way that is different from the


automatic conversion
ratio = female_number / male_number
Since female_number and male_number are integer in the
program, the decimal part of the result of the division
would be lost and ratio would represent a wrong figure
ratio = (float) female_number / male_number
The (float) operator converts the female_number to floating
point for the purpose of evaluation of the expression
This is called local conversion known as casing a
value
Syntax:
(type-name) expression
Type-name is one of the standard C data types
Expression may be constant, variable or an expression

Operator Precedence and


Associativity
Each operator in C has a precedence

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

frequently used in analysis of real-life


problems
Most of the C compilers support these
basic math function and are defined in
math library
To use any of these functions in a
program, we should include the line
#include <math.h>

THE END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy