0% found this document useful (0 votes)
5 views58 pages

388.Pdf - Displayname Operations & Expressions

Uploaded by

swanandk878
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views58 pages

388.Pdf - Displayname Operations & Expressions

Uploaded by

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

C Programming

Operators and Expressions


Introduction

An operator is a symbol that tells the computer to perform certain manipulations.


An expression is a sequence of operands and operators that reduces to a single value.
C operators can be classified into a number of categories.
Arithmetic operators
Relational operators
Logical operators
Assignment operators
Increment and decrement operators
Conditional operators
Bitwise operators
Special operators
Arithmetic operators

The arithmetic operators in C

Operator meaning
+ Addition or unary plus
- Subtraction or unary minus
* Multiplication
/ Division
% modulo division
Arithmetic operators

Integer division truncates remainder


The % operator cannot be applied to a float or double.
The precedence of arithmetic operators
Unary + or -
* / %
+ -
Arithmetic expressions

An expression is a sequence of operands and operators that reduces to a


single value.
Expressions can be simple or complex.
An operator is a syntactical token that requires an action be taken.
An operand is an object on which an operation that is performed; it
receives an operator’s action.
Arithmetic expressions

Example

axb–c a*b–c
(m + n) (x + y) (m + n) * (x + y)
(ab / c) a*b/c
3x2 +2x + 1 3*x*x+2*x+1
(x / y) + c x/y+c
Arithmetic Expression
Primary Expression

Primary Expression-Consist of only one


operand with no operator
Example a 5,’A’,calc,”Welcome”
Unary Expression

Consists of one operator and one operand

1. Unary Plus and Unary minus


Example
+a , +b,-c
2. Sizeof-Tells the size in bytes of a type or
primary expression
Example
Sizeof(int)
3. Cast Operator -Converts one expression
type to another
Example – Convert integer number to real
float(x)
Example for unary plus and minus Expression
Binary Expression

formed by operand-operator-operand combination

There are 2 types of Binary Expressions


1. Multiplicative- The multiplicative operators * / % group from left
to right Ex: a*b,a/b,a%b
2. Additive-the second operand is added to or subtracted from first
operand Ex: a+b,a-b
Binary Expressions
Binary Expressions (continued)
Binary Expressions (continued)
Relational Operators

The relational operators in C are :

Operator Meaning
< less that
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
Relational Operators

A relational expression yields a value of 1 or 0.


5<6 1
-34 + 8 > 23 - 5 0
if a=3, b=2, c =1; then a > b > c is ?
the associativity of relational operators is
left to right
Logical operators

C has the following three logical operators


&& meaning logical and
|| meaning logical or
! meaning logical not ( unary operator )
Expressions connected by && or || are evaluated left to right, and
evaluation stops as soon as the truth or falsehood of the result is known.
Assignment operators

• An assignment expression evaluates operand on right side of operator


= and places its value on left
• It has both value and side effect
• 2 forms of assignment
• Simple Assignment-found in algebraic expression.Ex a=b, b=x+1
• Compound assignment-shorthand assignment of simple assignment.
Demonstration of Compound Assignments
Demonstration of Compound Assignments
Demonstration of Compound Assignments
Increment and decrement operators

C provides two unusual operators for incrementing and decrementing


variables.
The increment operator ++ adds 1 to its operand, while the decrement
operator -- subtracts 1.
The unusual aspect is that ++ and -- may be used either as prefix operators
(before the variable, as in ++n), or postfix operators (after the variable:
n++).
In both cases, the effect is to increment n. But the expression ++n
increments n before its value is used, while n++ increments n after its value
has been used.
Increment and decrement operators

The increment and decrement operators can be used in complex statements.


Example:
m=n++ -j +10;

Consider the expression


m = - n++ ;
The precedence of ++ and – operators are the same as those of unary + and -.
The associatively of them is right to left.
m = - n++; is equivalent to m = - (n++)
Postfix Expression

• Postfix Expression-operator come after the operand

• Example x=a++;
Side effect and value of postfix of a++
Demonstrate Postfix Increment
Demonstrate Postfix Increment (continued)
Prefix Expression

In Prefix Expression operator come


before the operand

oExample x=++a ;
Side effect and value of prefix of a++
Demonstrate Prefix Increment
Demonstrate Prefix Increment (continued)
Evaluating Expressions
.
Expressions without Side Effects
Expressions with Side Effects
Evaluating Expressions
Evaluating Expressions
Evaluating Expressions
Evaluating Expressions

Given integer variables a, b, c, d, and e,


where a = 1, b = 2, c = 3, d = 4, evaluate the
following expressions:
a+b-c+d
a * b/c 1 + a * b % c
a +d%b -c
e=b=d+c/b–a
Conditional operator

a ternary operator pair “? : ” is available in C to construct conditional


expressions of the form
expr1 ? expr2 : expr3
the expression expr1 is evaluated first. If it is non-zero (true), then the
expression expr2 is evaluated, and that is the value of the conditional
expression. Otherwise expr3 is evaluated, and that is the value. Only one of
expr2 and expr3 is evaluated.
Example
z = (a > b) ? a : b; /* z = max(a, b) */
Example for ternary expression

#include <stdio.h> main()


{
int a , b; a = 10;
printf( "Value of b is %d\n", (a == 1) ? 20: 30 );
printf( "Value of b is %d\n", (a == 10) ? 20: 30 );
}
O/P
Value of b is 30 Value of b is 20
Special operators

1. The Comma Operator


The comma operator can be used to link the related expressions
together. A comma-linked list of expressions is evaluated left to right
and the value of right-most expression is the value of the combined
expression. For example, the statement
value = (x=10, y=5, x+y);
first assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 to
value. Since comma operator has the lowest precedence of all
operators, the parentheses are necessary.
Shift Operators

The shift operators move bits to the right or the left. When applied to
unsigned numbers, these operators are implementation independent.
When used with signed numbers, however, the implementation is left to the
discretion of the software engineer who designs the compiler.
Shift Operators

Shift-right Operation
Shift Operators

Divide by Shift
Shift Operators

Shift-left Operation
Shift Operators

Multiply by Shift
Int x=2;
Y =X>>3;
Z = x>>5;

A = X<<3;
B = x<<6;
Type conversions in expressions

1. Implicit Type Conversion


C permits mixing of constants and variables of different types in an
expression. C automatically converts any intermediate values to the proper
type so that the expression can be evaluated without loosing any
significance. This automatic conversion is known as implicit type conversion.
The rule of type conversion: the lower type is automatically converted to
the higher type.
Some Computational Problems

When expressions include real values, then it is important to take necessary


precautions to guard against certain computational errors. For example,
consider the following statements:
a = 1.0 / 3.0;
b = a * 3.0;
There is no guarantee that the value of b will equal 1.
Another problem is division by zero.
The third problem is to avoid overflow and underflow errors.
Operator precedence and Associativity

Rules of Precedence and Associativity


(1)Precedence rules decides the order in which different operators are
applied.
(2)Associativity rule decide the order in which multiple occurrences of
the same level operator are applied.
Table3.8 on page71 shows the summary of C Operators.
for example,
a = i +1== j || k and 3 != x
The sizeof operator

The sizeof operator operates on either a variable name, or a type name.


It gives the number of bytes occupied by the given variable, or any
variable of the given type respectively.
For example:
sizeof(int)
would have the value 4.
Type conversions

The constituent variables and constants in an expression can be of different


types. Then the question that naturally arises is what is the type of the value
of an expression.
For example, if in the expression a + b, a is of type int and b is of type
float, then what is the type of the sum?
Type conversion rules tell us what should be the type of the sum. We
address these questions in the next couple of slides.
Type conversions: cont'd.

Implicit type conversions


Such conversions in C take place in several contexts.
In arithmetic operations:
When operands of an arithmetic operator have different types.
For example, if a is of type int and b is of type char then the type of
the result of a + b is int.
The general rule here is that 'a lower' type is elevated to 'a higher' type
Type conversions: cont'd.

Across assignments:
In an assignment expression the right-hand side is first evaluated; the
returned value is the value of the left-hand side and also the value of the
assignment expression. The type of the left-hand side and that of the
assignment expression is the type of the right hand side.
For example,
if x is of type int and i is of char then as a result of the assignment x = i, x
becomes of type char.
Type conversions: cont'd

Explicit type conversions


This is done by using the cast operator. The syntax is :
(type-name) expression
The meaning is that the type of the expression is changed to the type
specified within round-brackets. For example,
sin((float) n)
converts the value of n to type float before passing it on to sin.
Implicit Explicit Type Convertion

Integer Data Types


short
int
long
Floating-Point Data Types
float
double
Conversion Between Primitive Data Types

For example:
int x;
double y = 2.5;
x = y;
This will cause an error
int x;
short y;
x = y;
This will NOT cause an error
…but, why?
Type Cast Operators

Type Cast Operators allow you to manually convert from one type to another, even if it is a
narrowing conversion.
Explicit convertion or typecasting
Example: x = (int)number;
If number is of a numeric data type, it will convert the value in number to the int type
and assigned to x.
If number is a floating-point type, the fractional part of the value would be lost int type
and assigned to x.
This is called truncation.
The value in number would not be changed.
Type Conversion in Arithmetic Operations

Recall integer division:


int number1 = 10, number2 = 4;
double number3 = number1 / number2;
number3 will have 2.0 stored in it as a result of the division because both operands of the division
are of type int.
We can use type casting on one of the operands to make sure the result is a double:
int number1 = 10, number2 = 4;
double number3 = (double)number1 / number2;
number3 will have 2.5 stored in it as a result of the division because one of the operands is of type
double.
Note that type casting operators can be applied to expressions enclosed in parentheses:
int number1 = 10, number2 = 4;
double number3 = (double)(number1 / number2);
number3 will have 2.0 stored in it as a result of the division because the type casting operator is
applied to the result of the integer division, which is 2

You might also like