0% found this document useful (0 votes)
14 views

C_Tutorial_Day_4

Uploaded by

Deepak
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)
14 views

C_Tutorial_Day_4

Uploaded by

Deepak
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/ 10

Programming With C-Operators and Expressions

OPERATORS AND EXPRESSIONS


An operator is a symbol that operates on a certain data type. For example + is an addition operator that op-
erates on numbers. An expression is the combination of operators and variables and/or constants. No two
operators comes together.
Types of Operators
Arithmetic Relational logical Assignment Increment and decrement
Conditional Bitwise comma Others

Arithmetic Operators
The arithmetic operators provide arithmetical operation and can be classified into unary and binary arithmetic
operators. It can operate on any built-in data type.
+ Addition or unary plus - Subtraction or unary minus
* multiplication / division % modulo division
C has no operators for exponentiation. We have to use pow(x,y) function which is available in math.h header
file. It returns xy .

Created and verified By: B. K. Deepak 29


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Arithmetic Expression
Arithmetical expression is of three types:
 Integer mode arithmetic: variables and constants used in operation are of integer type and will yield an
integer value.
 Real mode arithmetic: variables and constants used in operation are of real number and will yield real
value.
 Mixed mode arithmetic: variables and constants used in operation are of real type and integer type and
result will yield real type.

Assignment Statement
The result of an arithmetic expression assigned to a variable name using assignment operator ( = ). The va-
riable name on which result of an expression is to store is kept in the left side of assignment operator and
expression is on the right side of assignment operator. Statement is terminated by semicolon.

var_name=expression;

Created and verified By: B. K. Deepak 30


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Hierarchy of Arithmetical Operators


*/% - 1
+- - 2
for example expression 10/6+7/5 will be evaluated as follows:
10/6+7/5
1+7/5
1+1
2
But if expression contains the operators whose hierarchy is same then it will follow its associativity which is L
to R.
For example expression 18*5/6-30+5*6+4/3 will be evaluated as
90/6-30+5*6+4/3
15-30+5*6+4/3
15-30+30+4/3
15-30+30+1
-15+30+1
15+1
16

Created and verified By: B. K. Deepak 31


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Promotion and Type casting


Consider the example
int i;
float j;
i=10;
j=i;
Here value of i is stored in j which is a floating type variable and takes 4 byte. When the variable of lower
type converted to higher type is called promotion. Here value of i is implicitly changed to float type without
any loss in data.Consider another example
int m,n;
float k;
m=5;
n=2;
k=m/n;
here k will not store 2.5 because m/n will yield a value 2 because both are integer. Here we can use typecast
to get the proper result
k=(float)m/n;

Created and verified By: B. K. Deepak 32


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Relational Operators
If we have to take some decision, then we make a compare. This comparison is done by the help of relation-
al operators. It evaluates to 0 if compare results false and 1 if it is true.

Expression1 comp_operator Expression2


Operators are
< less than
<= less than or equal to
== equal to
>= greater than or equal to
> greater than
!= not equal to
let a=10 b=20 c=12.5 then what will be the output
d=a<b; d=a+c<b; d=a+c==b; d=a+c>b;

Created and verified By: B. K. Deepak 33


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Logical Operators
A logical operator is used to compare or evaluate logical and relational expression. There are three logical
operators in C language. They are
&& Logical AND
|| Logical OR
! Logical NOT
for example:
int year,n;
year=1980;
n=(year%4==0 && year%100!=0)||(year%400==0);
What will be the value of n?
Here year%4==0 evaluates to true and year%100!=0 evaluates to true therefore True && True will yield True
result and year%400==0 yield false value and now whole result will be True || false will yield the value True
i.e. 1.
int k,m;
k=0;
m=!k;
now the value of m will be 1 .

Created and verified By: B. K. Deepak 34


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Bitwise operator
A bitwise operator operates on each bit of data. These operators are used for testing, complimenting or shift-
ing bits to the right or left. Usually bitwise operators are not useful in case of float and double variable. Oper-
ators are
& bitwise AND | bitwise OR ^ bitwise XOR
<< left shift >> right shift ~ bitwise complement
Example: a 0000 0000 0000 1101
b 0000 0000 0000 0111
int a=13,b=7,c;
c=a & b; a&b 0000 0000 0000 0101

d=a | b; a|b 0000 0000 0000 1111

e=a ^ b; a^b 0000 0000 0000 1010


f=a<<2;
a<<2 0000 0000 0011 0100
g=a>>2;
a>>2 0000 0000 0000 0011
h=~a;
~a 1111 1111 1111 0010

Created and verified By: B. K. Deepak 35


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Increment and Decrement operators


The increment and decrement operators are very useful in C language. They are extensively used in for and
while loops.
var_name++; ++var_name;
var_name--; -- var_name;
++ operator add 1 to the operand and
-- operator subtract 1 from the operand.
When this operator use in the statement before the variable is known as prefix operator and when it is added
after the variable name is known as postfix operator.
int t, m=5;
t=++m;
The above statement evaluates as
m=m+1;
t=m;
after execution the value of t will be 6 and value of m will be 6.
int t, m=5;
t=m++;
The value of t will be 5 and m will be 6. (How?)

Created and verified By: B. K. Deepak 36


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Advance assignment operator


Other forms of assignment operator exists, that are obtained by combining various operators such as + - *
etc with = sign.
var1 op=expression;
The above statement is equivalent to var1=var1 op (expression); for example
int m=100,n=5,k=10;
m*=n+k;
After evaluation of above statement we will get value of m as 15000. First it will evaluate n+k i.e. 5+10 results
15 and then it will be multiplied by 100*15 which results in 1500. The other assignment operators are +=-=
/= %= |= &= ^= <<= >>=
Conditional Operator
It is also known as ternary operator. It consists of two symbols the question mark ( ? ) and the colon ( : ).
exp1 rop exp2 ? exp3 : exp4;
rop is relational operator that evaluates exp1 rop exp2 as true(non zero) or false(zero). If it evaluates as true
exp3 will be executed else exp4 will be executed.
int a=10,b=15,great;
great=a>b?a:b;
The value of great after evaluation will be 15.

Created and verified By: B. K. Deepak 37


Dated:20/12/2004
Rev:00
Programming With C-Operators and Expressions

Comma Operator
A set of expression separated by commas is a valid construct in C language. For example
int m,k;
m=(k=3,k+2);
The right hand side consists of two expression separated by comma. The first expression is k=3 and the
second is k+2. The expressions are evaluated from left to right . The first 3 is assigned to k and then k is in-
cremented by 2 so value of k will be now 5 hence the value of m will be evaluated as 5.

Other operators
Dot (.) operator and arrow( -> ) operator used for member selection and used with structure and union. * and
& is pointer and reference operator.
Execise
1. When the const qualifier useful?
2. What do you understand by operator precedence? Explain the associativity of unary operators.
3. Explain the behavior of right shift operator for signed and unsigned number.
4. What is enumeration constant?
5. What is meant by explicit typecasting?
6. Explain the 4 basic data types in C.

Created and verified By: B. K. Deepak 38


Dated:20/12/2004
Rev:00

You might also like