Professional Documents
Culture Documents
OPERATORS:
An Operator is a symbol that tells the compiler to perform certain mathematical or
logical manipulations. C language is rich in built-in operators.
1. Arithmetic operators
2. Relational operators
3. Logical operators
4. Assignment operators
5. Equality operator
6. Increment and Decrement operators
7. Conditional operators
8. Bitwise operators
9. Special operators
1. Arithmetic operators: C provides all the basic arithmetic operators. The operator +, -, *, %.
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo Division
Integer division truncates any fraction part. The modulo division produces the remainder of an
integer division.
Here a&b are variables and are known as operands. The modulo division operator % cannot be
used on floating point data.
a. Integer Arithmetic: When both the operands in a single arithmetic expression, the expression
is called an integer expression, and the operation is called integer arithmetic.
If a&b are integers, then for a=14 and b=4
a-b=10
a+b=18
a*b=56
a/b=3 (decimal part truncated)
a%b=2 (remainder)
During integer division, if both the operands are of the same sign, the result is truncated towards
zero. If one of them is negative, the direction of truncation is machine dependent.
During modulo division, the sign of the result is always the sign of the first operand.
-14%3=-2
-14%-3=-2
14%-3=2
14%3=2
b. Real Arithmetic: An Arithmetic operation involving only real operands is called real
arithmetic. A real operand may assume values either in decimal or exponential notation.
Ex:
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.
c. Mixed-mode Arithmetic: When one of the operands is real and the other is integer ,the
expression is called a mixed-mode arithmetic expression .Then the result is always is always a
real number.
15/10.0=1.5
15+10.0=25.0
2. Relational operator: We often compare two quantities, and depending on their relation, take
certain decisions. For ex: we may compare the age of 2 persons or the price of 2 items, and so
on. These comparisons can be done with the help of relational operators.
Operator Meaning
Ex: Write a C program to use various relational operators & display the return values.
#include<stdio.h>
void main()
{
printf(“Condition : Return values \n”);
printf(“\n10<=10:%5d”,10<=10);
printf(“\n10>=100:%5d”,10>=100);
printf(“\n10==10:%5d”,10==10);
printf(“\n5!=6:%5d”,5!= 6);
}
Output:
3. Logical Operators:
The logical operators && and || are used when we want to test more than one condition. It
is used to combine two or more relational expressions. Logical Operators are used in decision
making. Logical expression yields value 0 or 1 i.e., (Zero or One). 0 indicates that the result of
logical expression is TRUE and 1 indicates that the result of logical expression is FALSE.
Operator Meaning
An expression of this kind which combines two or more relational expressions is termed as a
logical expression.
Truth Table:
The logical expression yields value of one or zero according to the truth table.
Ex:
number<0 || number>100
#include<stdio.h>
void main()
printf(“\n!(8==8):%5d”,!(8=8));
Output:
4. Assignment Operators: Assignment Operators are used to assign the result of an expression
to a variable. We have seen the usual assignment operator ’=’.
v op=exp;
Where v is a variable,
exp is an expression and
op is binary arithmetic operator.
The operator op= is known as the shorthand assignment operator.
Ex:
x+=y+1;
is same as x=x+(y+1);
a+=1
a=a+1
a-=1
a=a-1
a=a*(n+1) a*=n+1
a/=n+1
a=a/(n+1)
a%=b
a=a%b
5. Increment and Decrement
Operators (or) Unary Operator: C has two very useful operators not generally found in other
languages. These are the increment and decrement operators.
++ and - -
The operator ++ add 1 to the operand.
The operator - - subtracts 1 to the operand.
Increment and decrement operators are divided into
post increment post decrement
pre increment pre decrement
Post Increment: A Postfix operator first assigns the value to the variable on left then increments
the operand.
a++
a++ is equivalent to a=a+1 or a+=1
Ex:
a=10
a=10
a++
a=11
Output: 200
210
Pre Increment: A Prefix operator first adds 1 to the operand and then the result is assigned to
the variable on left.
++a
Ex: a=10
a=11
++a
a=11
}
Output: 210
210
Post Decrement: A Postfix operator first assigns the value to the variable on left then
decrements the operand.
a--
a-- is equivalent to a=a-1 or a-=1
Ex: a=10
a=10
a--
a=9
Ex: Write a C program to illustrate the Post decrement
#include<stdio.h>
void main()
{
int x=10,y=20,z,a;
z=x*y--;
a=x*y;
printf(“%d%d”,z,a);
}
Output: 200
190
Pre decrement: A Prefix operator first subtracts 1 to the operand and then the result is assigned
to the variable on left.
--a
Ex: a=10
a=9
--a
a=9
Output: 190
190
Ex:
(3>2)?3:2;
True
Output: 3
56>6? printf(“True”):printf(“False”);
}
Output: True
7. Bitwise Operator:
Bitwise operators for manipulation of data at bit level. These operators are used for
testing the bits (or) shifting them right or left.
Bitwise operators may not be applied for float or double.
Bitwise operators only operate on integer operands. Such as int, short, long int, char.
Operator Meaning
^ Bitwise exclusive OR
~ One’s complement
Tabular representation:
1 1 1
1 0 0
0 1 0
0 0 0
Syntax: op1&op2
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
b=4
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
C=0
Some Ex:-
8&8=8
13&25=9
2&3=2
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter a,b”);
scanf(“%d%d”,&a,&b);
c=a&b;
Tabular representation:
1 1 1
1 0 1
0 1 1
0 0 0
Syntax: op1|op2
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
b=4
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
C=12
Some Ex:-
8|8=8
13|25=29
2|3=3
#include<stdio.h>
void main()
int a,b,c;
printf(“Enter a,b”);
scanf(“%d%d”,&a,&b);
c=a|b;
Tabular representation:
1 1 0
1 0 1
0 1 1
0 0 0
Syntax: op1^op2
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
b=2
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
0
Some Ex:-
8^2=10
13^25=20
2^3=?
EX:- Write a C program for Bitwise Exclusive OR operation between two integers.
#include<stdio.h>
void main()
int a,b,c;
printf(“Enter a,b”);
scanf(“%d%d”,&a,&b);
c=a^b;
Left shift:
The shift operators are used to move bit patterns either to the left or the right. The shift operators
are represented by the symbols << and >>.
1. The left shift operation causes all the bits in the operand op to be shifted to the left by s
positions.
2. The left most bits in the original bit pattern will be lost and the right most s bit positions
that are vacated will be filled with 0’s.
Ex:- n<<3
Let n=2
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0
Y=n*2s
Where n=number
Y=2*23
= 16
#include<stdio.h>
void main()
{
int x,y;
printf(“read x”);
scanf(“%d”,&x);
x<<3;
y=x;
Output: read x 2
Right shift:
1. The right most s bits will be lost. The left most s bit positions that are vacated will be
filled with zero.
2. If op is an unsigned integer. If the variable to be shifted is signed, then the operation is
machine dependent.
3. op and s are constants or variables. Two restrictions on value of s.
a. It may not be –ve.
b. It may not exceed the number of bits used to represent the left operand op.
Ex:- n>>2
Let n=8
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0
15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
Y=n/2s
Where n=number
Y=8/22
=2
#include<stdio.h>
void main()
int x,y;
printf(“read x”);
scanf(“%d”,&x);
x>>2;
y=x;
Output: read x 8
Bitwise complement operator: The complement operator (~) is a unary operator. Also called
one’s complement operator.
Op Op1
0 1
1 0
Ex:- x= 1001 0110 1100 1011
#include<stdio.h> #include<stdio.h>
void main() void main()
{ {
int a=10; int a=10;
a=~a; a=~a;
printf(“a=%d”,a); printf(“a=%u”,a);
} }
Output: a=-11 Output: a=65525
Special Operators: C Supports some special operators such as comma operator, sizeof
operator, pointer operators (& and *) and member selection operators (. and ->).
Special operators are known as separators or punctuators. Special operators are
Ampersand ( & )
It is also known as address operator. It is specified before the identifier name. i.e. variable name.
It indicates memory location of the identifier.
It is denoted by ‘&’.
When used as a prefix to a variable name ‘&’ operator gives the address of that variable.
#include<stdio.h>
void main()
{
int n=10;
printf(“\n value of n is : %d”,n);
printf(“\n value of &n is : %u”, &n);
}
Output
value of n is : 10
Value of &n is:1002
Asterisk ( * )
Braces ( { } )
The opening brace ( { ) and closing brace ( } ) specify the start and end of compound
statement in a function.
Brackets
Brackets [] also referred as array subscript operator. It is used to indicate single and multi
dimensional arrays.
Colon ( : )
Comma Operator ( , )
It is used to link expressions together. It is used together with variables to separate one
variable from another. It is used in for loop. It has the lowest precedence among operators
sum= (x=5,y=3,x+y);
The comma operator can be used to link the related expressions together.
Comma-linked lists of expressions are evaluated left to right and the value of right-most
expression is the value of the combined expression.
Ex:
value=(x=10,y=5,x+y);
Hash ( # )
Eg: #include<stdio.h>
Parenthesis ( () )
Parenthesis ( () ) is also known as function call operator. It is used to indicate the open
and end of function prototypes, function call, function parameters. Parentheses are used to group
expressions.
Semicolon ( ; )
Eg: g=d+h;
Sizeof Operator: The sizeof is a compile time operator and, when used with an operand, it
returns the number of bytes the operand occupies. The operand may be a variable, a constant or a
data type.
Ex:
m=sizeof(int);
Ex: Write a C program to show the usage of sizeof operator.
#include<stdio.h>
void main()
{
int a=10;
float b=20.7;
printf(“%d%d”,sizeof(a),sizeof(b));
printf(“Address of a=%u,Address of b=%u”,&a,&b);
}
Output: 2
4
4096
5000
EXPRESSIONS:
An expression is a combination of variables, constants and operators arranged as per the
syntax of the language.
Evaluation of Expression:
Expressions are evaluated using.
variable =expression;
Simple Expression: An expression that has only one operator is called Simple Expression.
Example: a=a+2;
Compound Expression: While an expression that involves more than one operator is called Compound
expression.
Example: b=2+3*5;
The evaluation of simple expression is easier then compound expression, because we have more
operators in compound expression, while evaluating compound expression we must determine the order
in which operators will operate.
For example, to determine the result of evaluation of the expression b=2+3*5, we must determine the
order in which =,+ and * or *,+ and = will operate in these cases the result may be different. To avoid this
situation the operators will operate depends upon the Precedence and the Associativity of the operators.
These expressions are to be evaluated with the help of operators priority. i.e; precedence of
operators & associativity.
Precedence of operators:
Each operator in C has precedence associated with it. In compound expression, if the operators involved
are of different precedence, the highest precedence operator will operate first.
Associativity of Operators:
In a compound expression, when several operators of the same precedence appear together, the operators
are evaluated according to their Associativity. An operator can be either left – to – right associative or
right – left – associative.
The operators with the same precedence always have the same associativity. Let us look at various
operators, their classification, precedence and associativity.
+ Unary plus
- Unary minus
++ Increment
-- Decrement
! Not operator
~ One’s complement
* Pointer operator Right to left 2nd
& Address operator
Sizeof Size of an object
Type Typecast
* Multiplication
/ Division Left to right 3rd
% Modular division
Ex:
x=a-b/3+c*2-1
t1
step1 - a-t1+c*2-1
t2
step2 - a-t1+t2-1
t3
step3 - t3+t2-1
t4
Step4- t4-1
Step 5- t5
t5
Ex: x=a-b/3+c*2-1
Here a=9, b=12, c=3 Assumed.
X=9-12/3+3*2-1
=9-4+3*2-1
=9-4+6-1
=5+6-1
=11-1
=10