Operators
Operators
Operators
Operator: 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. The data items that
operators act upon are called operands.
Some operators require two operands, while others act upon only one operand. The operators are
classified into unary, binary and ternary depending on whether they operate on one, two (or) three operands
respectively.
Types of operators:
In ‘c’ operators can be classified into various categories based on their utility and action.
A list of operators types is given below
• Arithmetic operators.
• Relational operators.
• Logical operators.
• Assignment operators.
• Increment and Decrement operators.
• Conditional operators.
• Bitwise operators.
• Comma operators.
Arithmetic operators: it can perform arithmetic operations and can be classified into unary and binary
arithmetic operators. It can operate on any built-in data type.
Note: - c has no operator for exponentiation. A function pow(x,y) exists in math.h which returns xy
The unary – operator has the effect of multiplying operand by -1.
Integer arithmetic: - when 2 operands such as x and y are declared as integers, an arithmetic operation
performed on these integers is called Integer arithmetic. It always yields an integer value.
Ex: (1) Int x=16, y=5; Ex: 6/8 = 0
X+y = 21 -6/-8 = 0
x-y = 11 -6/8 = 0 (or) -1
x*y = 80
x/y = 1
(1) #include<stdio.h>
main()
{
int month,days;
printf(“enter the number of days \n”);
scanf(“%d”,&days);
months=days/30;
days=days%30;
printf(“ months is %d days are %d”,months,days);
}
VVSV 23
C Programming Lecture Notes
Floating Point arithmetic: - (FPA) Floating point arithmetic involves operands of real type in decimal
or exponential notation. The remainder operator % is not applicable to FPA.
Ex : - float a=14.0, b= 4.0;
P = a/b = 3.5000
Q= b/a = 0.285714
R = a+b = 18.0000
Mixed Mode arithmetic:- in this mode, if either of the operands is real, the resultant value is always a
real value.
Ex: 35/5.0 = 7.0
Here 5.0 is a double constant, 35 is converted to a double and result is also a double.
Relational Operators:-
Arithmetic operators are used to evaluate arithmetic expressions, relational operators are used to
compare arithmetic, logic and character expressions.
Each of these operators compares their left hand side with their right hand side.
The whole expression involving the relational operator then evaluates to an integer.
It evaluates 0 if the condition is false, and 1 if it is true.
Operator Meaning
< Lessthan
<= Lessthan (or) Equal to
> Greaterthan
>= Greaterthan(or)Equal to
== Equal to
!= Not Equal to
Ex:- #include<stdio.h>
main()
{
int I,j;
printf(“enter two values \n”);
scanf(“%d %d”,&I,&j);
if(i==j)
pritf(“both are equal \n”);
else
printf(“both are not equal \n);
}
Logical operators:
it is used to compare or evaluate logical and relational expressions. There are 3 logical operators in c.
Operator meaning
An expression involving && and || is some times called compound expression, since the expression
involves two other expressions, i.e. each of these operators (&& and ||) take two expressions, one to the left
and another to right.
Logical NOT:- the ! Operator tales single expression and evaluates to true if the expression is false, and
evaluates to false if the expression is true.
VVSV 24
C Programming Lecture Notes
The ! Operator is convict to use when you want to test whether the value of a variable is zero.
If(!i)
Printf(“the value of I is zero \n);
The increment and decrement operators are very useful in ‘c’ language. These are extensively used in for
and while loops.
The syntax is ++<variable> --<variable>
<variable>++ <variable>--
The operator ++ adds 1 to the operand and – subtracts 1 from the operand. These operands manifest in
two forms i.e. prefix and postfix.
Ex: the ++ operator can be used in two ways.
++m and m++ (these two are different operations).
The expression ++m will increment first, and then this value is assigned to n, resulting in having the same
value.
main()
{
int n,m=1;
n=m++;
printf(“ postfix operation :n=%d”,m=%d\n”,n,m);
}
postfix operation n=1,m=2
The expression m++ will first evaluate the value of m, resulting is 1 being assigned to n and then the value
of m being incremented to 2.
Bitwise operators:
A bitwise operator operates on each bit of data. These operators are used for testing, complementing or
shifting bits to the right or left.
Usually these operators are not useful in cases of float and double variables.
Operator Meaning
& Bitwise AND.
| Bitwise OR.
^ Bitwise XOR.
<< Left Shift (Shift Left).
>> Shift Right.
~ compliment.
Bitwise AND: This operator produces a 1 only if both bits in an operation are 1, else it produces a 0. For
example, suppose that a=13 and b=25. If we have c= a&b, the following result will be produced.
VVSV 25
C Programming Lecture Notes
----------------------------------
a&b= 0000 0000 0000 1001 (9)
----------------------------------
Bitwise OR: This operator produces a 1 if both one or both in an operation are 1, else it produces a 0. For
example, suppose that a=13 and b=25. If we have c= a|b, the following result will be produced.
Bitwise XOR: This operator produces a 1 only if one bit in an operationis 1 and the other bit is 0, else it
produces a 0. For example, suppose that a=13 and b=25. If we have c= a^b, the following result will be
produced.
<<(Left Shift) Operator: This operator shifts the bit positions of a variable to left by n positions. For
example, if a=13 and we have b=a<<1, the following is the result
>>(Right Shift) Operator: This operator shifts the bit positions of a variable to right by n positions. For
example, if a=26 and we have b=a>>1, the following is the result
Thus, we can see that left shift by 1 is actually the same as multiplication by 2, and right shift by 1
position is equivalent to division by 2.
a= -3 0011
1100
------
1
------
1101
------
(1) To find 2’s complement
Complement to the given number.
And add 1 to complement number.
VVSV 26
C Programming Lecture Notes
~(One’s complement) operator: This operator reverses each bit(i.e., changes each 0 to 1, and vice
versa). Thus, if a=13 and we have b=~a, the following happens
Conditional Operator (ternary operator)(?:): C includes a very special operator called ternary (or)
conditional operator. It is called ternary because it uses three expressions. The ternary operator acts like a
shorthand version of the if-else construction.
Larger = i > j ? i : j ;
Here i > j is condition for testing
? mark is conditional operator
i is for expression1.
j is for expression2.
Special operators:-
Comma operator:- A set of expressions separated by comma is a valid construct in the ‘c’ language.
Ex: int i , j;
Here i and j are declared as integers.
Consider the following statement that makes use of the comma operator.
i = ( j = 3 , j + 2);
the right hand side consists of two expressions separated by comma. These expressions are evaluated from
left to right.
First the value 3 is assigned to j and then the expression j + 2 is evaluated.
The value of the entire comma-separated expression is the value of the right most expression. Hence the
value assigned to i would be 5.
Ex: c= ( a = 10, b =20 , a + b);
First a =10 , b = 20 and a + b = 30 is assigned to c.
Other Operators:
VVSV 27
C Programming Lecture Notes
Sizeof: The operator sizeof gives the datatype or the variable in terms of bytes occupied in the memory. The
sizeof operator returns the no. of bytes the operand occupies in memory. The operand may be a variable, a
constant or a data type.
&(Address of) operator:- The address of operator (&) returns the address of the variable. The operand
may be a variable, a constant
m= &n;
here address of n is assigned to m. this m is not an ordinary variable, it is a variable which holds address of
another variable.
*(Value at address) operator:- The value at address operator (*) returns the value stored at a particular
address. The value at address operator is also called indirection operator.
Ex: x = *m;
Here the value at address of m is assigned to x and m is going to hold the address.
TYPE CONVERSION:
The process of converting one data type value into another data type is called Type conversion. This is two
types
(1) Implicit / coercion.
(2) Explicit / Type casting.
Implicit type conversion:- The process of converting one type value into another value implicitly by the
compiler is called implicit type conversion (or) coercion.
Explicit type conversion:- The process of converting one type into another value explicitly by
programmer is called Explicit type conversion (or) type casting.
Type cast operator:- ‘c’ permits mixing of constants and variables of different types in an expression, but
during evaluation, it follows a definite rule of type conversion.
In an expression, if two operands are of different types, then the lower type value is automatically converted
in higher type. This process is implicit type conversion (or) automatic type conversion (or) type coercion, but
in some situations we need to convert one type value into another explicitly. This process is known as explicit
type conversion (or) type casting.
Syntax: ( type name) expression;
main() main()
{ {
int a; char x;
a=(int)’a’; x=(char)65;
printf(“ %d”,a); printf(“ %c”,x);
} }
O/P – 97 O/P – A
main()
{
int a=10;float b=3.14;
printf("value of a is %d \n",a);
printf("address of a is %u \n",&a);
printf("value of b is %f \n",b);
printf("address of b is %u \n",&b);
}
OUTPUT :-
value of a is 10
address of a is 3221207028 (Assumption)
value of b is 3.140000
address of b is 3221207024( Assumption).
VVSV 28
C Programming Lecture Notes
main()
{
int a=10;float b=3.14;
printf("value of a is %d \n",*(&a));
printf("address of a is %u \n",&a);
printf("value of b is %f \n",*(&b));
printf("address of b is %u \n",&b);
}
OUTPUT:-
value of a is 10
address of a is 3221210948
value of b is 3.140000
address of b is 3221210944
Assume ‘k to be an integer variable and ‘a to be float variable then the values of k and a for the following
are
OUTPUT:
a=10
a=11
a=12
VVSV 29
C Programming Lecture Notes
a--;
printf("a=%f\n",a);
}
OUTPUT:
a=10.900000
a=9.900000
a=8.900000
OUTPUT:
a=11 b=10
a=12 b=12
OUTPUT:
a=8.900000 b=9.900000
a=9.900000 b=8.900000
6) Write a program for sizeof() operator.
main()
{
int a=10;float b=5.6; char c='*';
printf("size of(a)=%d\n",sizeof(a));
printf("size of(b)=%d\n",sizeof(b));
printf("size of(c)=%d\n",sizeof(c));
}
OUTPUT :
size of(a)=4
size of(b)=4
size of(c)=1
VVSV 30
C Programming Lecture Notes
main()
{
int a=10,b=20;
printf("a<=b:%d",a<=b);
}
OUTPUT:
a<=b:1
main()
{
int a=20,b=20;
printf("a==b:%d",a==b);
}
OUTPUT:
a==b:1
main()
{
int a=-10;
printf("a=:%d\n",a);
printf("!a=%d\n",!a);
printf("!!a=%d\n",!!a);
}
OUTPUT:
a=:-10
!a=0
!!a=1
long int l;
x=l/i+i*f–c
VVSV 31
C Programming Lecture Notes
a) if unsigned int can be converted to long int, the unsigned int operand will be converted as such and
the result will be long int.
b) else both operands will be converted to unsigned long int and the result will be unsigned long int.
6) else if one of the operands is long int, the other will be converted to double and the result will be
long int.
7) else if one of the operands is unsigned int, the other will be converted to double and the result will
be unsigned int.
example:
RL
! Logical NOT
RL
~ One’s compliment
RL
- Unary minus
RL
++ Increment
RL
2 -- Decrement
RL
& Address of
RL
* Indirection
RL
(data_type) Cast operator
RL
sizeof( ) Sizeof special operator
RL
* Multiplication LR
3 / Division LR
% Modulus LR
VVSV 32
C Programming Lecture Notes
+ Addition LR
4
- Subtraction LR
< LR
Less than
> LR
Greater than
6 <= LR
Less than or equal to
Grater than or equal to
>= LR
== Equal to LR
7
!= Not equal to LR
= += -= *= /=
14 %= >>= <<= &= Simple and compound assignment RL
^= |=
15 , Comma LR
Ex: x = 20, y = 5;
If ( x == 10 + 15 && y <10)
If( x == 25 && y<10)
VVSV 33
C Programming Lecture Notes
Preprocessor Directives:- Preprocessor directives are instructions given by us to the compiler and are
executed before the process of compilation
(OR)
A c-preprocessor is a program that processes our source program before it is passed to the compiler.
Preprocessor commands are also known as preprocessor directives. they begin with # symbol. Some of the
preprocessor directives are
(1) macro expansion.
(2) file inclusion.
write a program to find min of two numbers if first no is less than the second no. then print ‘1’ other
wise print ‘0’.
A) #include<stdio.h>
#define true 1
#define false 0
main()
{
int a,b;
printf("enter two numbers\n");
scanf("%d %d",&a,&b);
(a<b)?printf("%d",true):printf("%d",false);
}
OUTPUT:
enter two numbers
3
5
1
OUTPUT:
enter two numbers
5
3
0
#include<stdio.h>
#define AND &&
#define OR ||
main()
{
int a=10,b=20,c;
c=a>b AND b<=20 OR a!=10;
printf("%d",c);
}
OUTPUT:
0
#include<stdio.h>
#define P printf
#define S scanf
main()
{
int a;
VVSV 34
C Programming Lecture Notes
P("enter a number\n");
S("%d",&a);
P("a=%d",a);
}
OUTPUT:
enter a number
9
a=9.
OUTPUT:
HELLO GOOD MORNING
HELLO GOOD MORNING
#include<stdio.h>
#define LINE printf("one\n");printf("two\n");printf("three\n");
main()
{
LINE;
}
OUTPUT:
one
two
three
#include<stdio.h>
#define LINE {printf("one\n");printf("two\n");printf("three\n");}
main()
{
LINE;
}
OUTPUT:
one
two
three
File Inclusion:
#include directive is used for file inclusion.
#include directive causes of one file to be included in another file.
In general we write
#include<stdio.h> or #include”stdio.h”
This statement causes entire contents of the file to be replaced in the program at that point. In general a file
to be included is having .h extension.
VVSV 35
C Programming Lecture Notes
Files to be included in our program are header files, because they contain statements which when declared
replaces their contents at the beginning of our program.
Preprocessor directives can be placed at anywhere in the program, but in general, they are placed at
beginning of the program.
#include<math.h>
main()
{
float p,r,ci,si;
int n,t;
printf("Enter Principle Amount\n");
scanf("%f",&p);
printf("Enter the rate of Interest\n");
scanf("%f",&r);
printf("Enter the number of years\n");
scanf("%d",&n);
printf("Enter the number of times the interest compounded per year\n");
scanf("%d",&t);
si=(p*n*r)/100;
ci=p*pow(1+(r/n),n*t);
//p=principle amount
//r=annual nominal interest rate(as decimal)--r/100
//n=the number of time the interest is compounded per year
//t=the number of years
//ci=amount after time t
//sample data p=1500,r=0.043,n=4,t=6then ci=1938.84
printf("the simple interest is %f\n",si);
printf("The compound Interest is %f\n",ci);
}
OUTPUT:-
Enter Principle Amount
1500
Enter the rate of Interest
043
Enter the number of years
4
Enter the number of times the interest compounded per year
6
the simple interest is 2.580000
The compound Interest is 1938.836792
VVSV 36
C Programming Lecture Notes
}
OUTPUT:
enter temperature in celcius
10
eqivalent fahrenheit is 50.000000
enter the temperature in fahrenheit
50
eqivalent celcius is 10.000000
OUTPUT:
enter a charecter
r
114 is not a digit.
4) Write a program to check the given character is in upper case . if it is in upper case then it convert
into lowercase.
main()
{
char ch;
printf("enter a charecter\n");
scanf(" %c",&ch);
(ch>=65 &&ch<=90)?printf(" %c is in upper case\n",ch):printf(" %c is not in upper case\n",ch);
ch=ch+32;
printf("after conversion in lower case ch=%c\n",ch);
}
OUTPUT:
enter a charecter
S
VVSV 37
C Programming Lecture Notes
S is in upper case
after conversion in lower case ch=s
OUTPUT:
enter a charecter
k
k is not in upper case
after conversion in lower case ch=ï
#include<stdio.h>
main()
{
char ch;
printf("enter a chrecter \n");
scanf(" %c",&ch);
ch=ch+32;
printf("after conversion ch=%c\n",ch);
}
OUTPUT:
enter a chrecter
K
after conversion ch=k
main()
{
char ch;
printf("enter a chrecter \n");
scanf(" %c",&ch);
ch=ch-32;
printf("after conversion ch=%c\n",ch);
}
OUTPUT:
enter a chrecter
k
after conversion ch=K
7) main()
{
int a=65;
printf(“value of a is =%d”,a);
printf(“corresponding character is a=%c”,ch);
}
OUTPUT:
a=65
A
VVSV 38