0% found this document useful (0 votes)
9 views9 pages

C Operators: Types and Examples

The document provides an overview of operators in the C programming language, detailing their types including arithmetic, relational, logical, bitwise, assignment, and special operators. It includes examples of how these operators are used in C programs, along with their syntax and outputs. Additionally, it explains increment and decrement operators, highlighting their functionality in manipulating variable values.

Uploaded by

MALARMANNAN A
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)
9 views9 pages

C Operators: Types and Examples

The document provides an overview of operators in the C programming language, detailing their types including arithmetic, relational, logical, bitwise, assignment, and special operators. It includes examples of how these operators are used in C programs, along with their syntax and outputs. Additionally, it explains increment and decrement operators, highlighting their functionality in manipulating variable values.

Uploaded by

MALARMANNAN A
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

Operator in C

An operator is a symbol that tells the compiler to perform a certain mathematical or


logical manipulation. Operators are used in programs to manipulate data and variables.
 The symbols which are used to perform logical and mathematical operations in a C
program are called C operators.
 These C operators join individual constants and variables to form expressions.
 Operators, functions, constants and variables are combined together to form
expressions.
 Consider the expression A + B * 5. where, +, * are operators, A, B are variables, 5
is constant
Types of operators

 Arithmetic operators
 Relational operators
 Logical operators
 Bitwise operators
 Assignment operators
 Conditional operators
 Special operators
Arithmetic operators
These are used to perform mathematical calculations like addition, subtraction,
multiplication, division and modulus. C supports all the basic arithmetic operators.
The following table shows all the basic arithmetic operators.

Operator Description

+ adds two operands

- subtract second operands from first


* multiply two operand

/ divide numerator by denominator

% remainder of division

++ Increment operator - increases integer value by one

-- Decrement operator - decreases integer value by one

Example Program:
1.// Arithmetic operators

#include <stdio.h>
int main()
{
int a=40,b=20, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of a, b is : %d\n", add);
printf("Subtraction of a, b is : %d\n", sub);
printf("Multiplication of a, b is : %d\n", mul);
printf("Division of a, b is : %d\n", div);
printf("Modulus of a, b is : %d\n", mod);
}

OUTPUT
Addition of a, b is : 60
Subtraction of a, b is : 20
Multiplication of a, b is : 800
Division of a, b is : 2
Modulus of a, b is : 0
Relational operators

Relational operators are used to find the relation between two variables. i.e. to compare
the values of two variables in a C program.

Operator Description

== Check if two operand are equal

!= Check if two operand are not equal.

> Check if operand on the left is greater than operand on the right

< Check operand on the left is smaller than right operand

>= check left operand is greater than or equal to right operand

<= Check if operand on left is smaller than or equal to right operand

// Working of relational operators


#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}

OUTPUT:
m and n are not equal
Logical operators

These operators are used to perform logical operations on the given expression. There are
3 logical operators in C language. They are, logical AND (&&), logical OR (||) and
logical NOT (!).

Operator Description Example

&& Logical AND (a && b) is false

|| Logical OR (a || b) is true

! Logical NOT (!a) is false

// Working of Logical operators

#include <stdio.h>
int main()
{
int m=40,n=20;
int o=20,p=30;
if (m>n && m !=0)
{
printf("&& Operator : Both conditions are true\n");
}
if (o>p || p!=20)
{
printf("|| Operator : Only one condition is true\n");
}
if (!(m>n && m !=0))
{
printf("! Operator : Both conditions are true\n");
}
else
{
printf("! Operator : Both conditions are true. " \
"But, status is inverted as false\n");
}}
OUTPUT:
&& Operator : Both conditions are true
|| Operator : Only one condition is true
! Operator : Both conditions are true. But, status is inverted as false
Bitwise operators
These operators are used to perform bit operations. Decimal values are converted into
binary values which are the sequence of bits and bit wise operators work on these bits.

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift

Truth table for bit wise operation & bit wise operators:

Left-shift operator

It is an operator that shifts the number of bits to the left-side.

Syntax of the left-shift operator is given below:

Operand << n

Where,

Operand is an integer expression on which we apply the left-shift operation. N is the


number of bits to be shifted.

In the case of Left-shift operator, 'n' bits will be shifted on the left-side. The 'n' bits on the
left side will be popped out, and 'n' bits on the right-side are filled with 0
Right-shift operator

It is an operator that shifts the number of bits to the right side.

Syntax of the right-shift operator is given below:

Operand >> n;
Where,
Operand is an integer expression on which we apply the right-shift operation. N is the
number of bits to be shifted.

In the case of the right-shift operator, 'n' bits will be shifted on the right-side. The 'n' bits
on the right-side will be popped out, and 'n' bits on the left-side are filled with 0.

Consider x=40 and y=80. Binary form of these values are given below.
x = 00101000
y= 01010000
// Working of Bitwise operators

#include <stdio.h>
int main()
{
int m = 40,n = 80,AND_opr,OR_opr,XOR_opr,NOT_opr ;
AND_opr = (m&n);
OR_opr = (m|n);
XOR_opr = (m^n);
printf("AND_opr value = %d\n",AND_opr );
printf("OR_opr value = %d\n",OR_opr );
printf("XOR_opr value = %d\n",XOR_opr );
printf("left_shift value = %d\n", m << 1);
printf("right_shift value = %d\n", n >> 1);
}
OUTPUT:
AND_opr value = 0
OR_opr value = 120
XOR_opr value = 120
left_shift value = 80
right_shift value = 20
Assignment Operators

These are used to assign the values for the variables in C programs. Assignment
operators supported by C language are as follows.

Operator Description Example

assigns values from right side operands to left side


= a=b
operand
adds right operand to the left operand and assign the a+=b is same as
+=
result to left a=a+b
subtracts right operand from the left operand and assign a-=b is same as
-=
the result to left operand a=a-b
mutiply left operand with the right operand and assign the a*=b is same as
*=
result to left operand a=a*b
divides left operand with the right operand and assign the a/=b is same as
/=
result to left operand a=a/b

calculate modulus using two operands and assign the a%=b is same as
%=
result to left operand a=a%b

//Assignment operators

#include <stdio.h>
int main()
{
int a = 5, c;
float b=10.01,d;

c = a;
printf("c = %d\n", c);

d=b;
printf(“d=%f\n”, d);

return 0;
}
Output
c=5
d = 10.010000
Special operator

Operators Description

This is used to get the address of the variable.


&
Example : &a will give address of a.

This is used as pointer to a variable.


*
Example : * a where, * is pointer to the variable a.

This gives the size of the variable.


Sizeof ()
Example : size of (char) will give us 1.

//Special operator

#include <stdio.h>
#include <limits.h>
int main()
{
int a;
char b;
float c;
double d;
printf("Storage size for int data type:%d \n",sizeof(a));
printf("Storage size for char data type:%d \n",sizeof(b));
printf("Storage size for float data type:%d \n",sizeof(c));
printf("Storage size for double data type:%d\n",sizeof(d));
return 0;
}
OUTPUT:
Storage size for int data type:4
Storage size for char data type:1
Storage size for float data type:4
Storage size for double data type:8
Increment and decrement operators

These operators are used to either increase or decrease the value of the variable by one.

Operator Operator/Description
Pre increment operator value of i is incremented before assigning it to the
(++i) variable i
Post increment operator value of i is incremented after assigning it to the
(i++) variable i

value of i is decremented before assigning it to the


Pre decrement operator (–i) variable i

Post decrement operator (i– value of i is decremented after assigning it to


) variable i

// Increment and decrement operators

#include <stdio.h>

int main() {

int var1 = 5, var2 = 5;

printf("%d\n", var1++);

printf("%d \n",var1);

printf("%d\n", ++var2);

return 0;

}
Output
5
6
6

You might also like