4 Types of Operators in C

Preview

Operators

These are the symbols which are used to perform some calculation or manipulation on values which may be in the form of variables, constants or expressions in a C program.

Operand

It is the variable, constant or expression on which an operator performs some calculation, comparison or some other manipulation.
Example 
  6+5      
In the above example, 6 and 5 are operands. + is the operator which is performing addition of 6 and 5

Types of Operators in C

Operators are of various categories as

  • Unary Operators
  • Binary Operators
  • Ternary Operator
  • Special Operators

1. Unary Operators

Unary operators are those operators which have single operand. They are of following types:

a. Unary Plus (+)

It is used to specify that the number is positive. It is an optional sign because a number without any sign is automatically considered positive.

Example

+6, 45

b. Unary Minus (-)

It is used to specify that the number is negative.

Example
  -6, -45

c. Pointer Operator (*)

It is known as pointer or pointer to operator. It is used to declare and use the pointer variable.

Example:
int *ptr;
**Above statement declares a pointer variable ptr of int type.

d. Address operator(&)

It is also known as “address” operator. It is used to store the address of a variable in pointer variable.

Example

int num=10,*ptr;
ptr=#

**Above statement stores the address of variable num in pointer variable ptr.

e. Increment (++) /Decrement (–) Operators

2. Binary Operators

These are the operators which can have two operands. Various categories of binary operators in C language are:-

 

3. Ternary Operator/Conditional operator

 

 Special operators

These operators are used for special purposes. There are many special operators provided by C language which are as follows:

  •  sizeof()
  •  Comma

 a.  sizeof()

This operator is used to find the size of a variable, constant or data type. The size returned by this operator is in terms of bytes.

 

Program to demonstrate the use of sizeof() operator.

#include<stdio.h>
#include<conio.h>
main()
{
int a;
float b;
char c;
clrscr();
printf(“\nsizeof(a)=%d”,sizeof(a));
printf(“\nsizeof(b)=%d”,sizeof(b));
printf(“\nsizeof(c )=%d”,sizeof(c));
printf(“\nsizeof(double)=%d”,sizeof(double));
printf(“\nsizeof(long double)=%d”,sizeof(long double));
printf(“\nsizeof(short)=%d”,sizeof(short));
printf(“\nsizeof(123)=%d”,sizeof(123));
printf(“\nsizeof(123l)=%d”,sizeof(123l));
printf(“\nsizeof(123.45)=%d”,sizeof(123.45));
printf(“\nsizeof(123.45f)=%d”,sizeof(123.45f));
printf(“\nsizeof(123.45L)=%d”,sizeof(123.45L));
getch();
}

Output:
sizeof(a)=2
sizeof(b)=4
sizeof(c )=1
sizeof(double)=8
sizeof(long double)=10

sizeof(short)=2

sizeof(123)=2

sizeof(123l)=4

sizeof(123.45)=8

sizeof(123.45f)=4

sizeof(123.45L)=10

** In the above program, a is a variable of int type. Size of int is 2. So size of variable a would be 2. Float type variable b is of 4 bytes. Char type variable c is having size 1. Similarly size of other data types  are displayed. Sizeof(123) is 2 because 123 is int value. Sizeof(123l) is 4 because alphabet l has been suffixed to the value. Sizeof(123.45) is 8 as real values are by default double type values. After suffixing f with the real value, value is converted into float type. By suffixing L with the real value, value become long double type value.

 b. Comma operator

This operator is used to separate variables from each other in variable declaration statement and to separate multiple expressions from each other in same line. It is also used in library functions like printf() and scanf() to separate control string and arguments.

  

 Program to demonstrate the use of comma operator.

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

c=(a=5,b=4,a+b);

printf(“\na=%d”,a);

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

printf(“\nc=%d”,c);

getch();

}

Output:

a=5

b=4

c=9

** In line6, in expression c=(a=5,b=4,a+b), a will be assigned value 5, b will be assigned value 4, then a+b will be evaluated to generate sum of 5 and 4 i.e. 9 which will be stored in variable c.

 PRECEDENCE AND ASSOCIATIVITY OF OPERATORS

Precedence is the way to provide a priority to every operator. When more than one operator appear in an expression, they will be evaluated depending upon their precedence i.e. the operator with higher precedence will be evaluated first followed by the operator with lower precedence and so on.

For example, we want to calculate arithmetic expression 9+4/2. In this expression, the division operator (/) has more priority as compared to addition operator (+), so 4/2 will be solved first, then result of 4/2 will be added to 3 i.e. 2 will be added to 9 and output of expression would be 11.

In C language, priority to operators is assigned in the form of levels from 1 to 15, parenthesis and square brackets have the highest priority level and comma operator is having the lowest priority.

Associativity is the way to specify the order in which operators will be evaluated if more than one operator of same precedence appears in an expression. Associativity can be either “Left to Right” or “Right to Left”. Associativity is predefined in C language. While solving an expression we must consider associativity along with precedence to solve an expression accurately.

TABLE TO SHOW PRECEDENCE AND ASSOCIATIVITY OF OPERATORS

lEVEL no

OPERATORS

ASSOCIATIVITY

1(), []Left to Right
2!, ~, ++, –, Unary +, Unary -, *, &, (type), sizeof() )Right to Left
3*, /, %Left to Right
4+ , –Left to Right
5<< , >>Left to Right
6<, <= , > , >=Left to Right
7= =, !=Left to Right
8&Left to Right
9^Left to Right
10|Left to Right
11&&Left to Right
12 ||Left to Right
13?:Right to Left
14=, +=, -=, /=, *=, ^=, !=, ,=, >>=Left to Right
15COMMA OPERATOR(,)Left to Right

 Expression

It is the combination of operators and operands. Expression is of various types:

a.Arithmetic expression

The expression which contains arithmetic operators is known as arithmetic expression.

Example:

9+5-2*4

b. relational expression:

The expression which contains relational operators is known as relational expression. The Output: of a relational expression is always 1 or 0.

Example:

9<=4

c. LOGICAL expression:

The expression which contains logical operators is known as logical expression. The Output: of a logical expression is always 1 or 0.

Example:

9<=4 && 3<6

d. BITWISE expression:

The expression which contains bitwise operators is known as bitwise expression.

Example:

10&7

e. conditional expression:

The expression which contains conditional operator is known as conditional expression.

Example:

c=a>b?a:b;

 IMPLICIT COVERSION/AUTOMATIC CONVERSION

Implicit conversion means automatic conversion. It is the way in which the output of an arithmetic expression is converted automatically into a particular data type. The basic rule of implicit conversion is that the output of an expression will be of that data type which has the highest number of bytes and range. For example if one operand is of int type and other of float type, the output will be of float type as float type has larger size and range of values compared to int data type. There are some rules of implicit conversion which are as follows:

Data type of first operand

Data type of second operand

Data type of Output

shortshortshort
shortintint
shortlonglong
shortfloatfloat
shortdoubledouble
shortlong doublelong double
shortcharshort
Intintint
intlonglong
intfloatfloat
intdoubledouble
intlong doublelong double
intcharint
longlonglong
longfloatfloat
longdoubledouble
longlong doublelong double
longcharlong
floatfloatfloat
floatdoubledouble
floatlong doublelong double
floatcharfloat
doubledoubledouble
doublelong doublelong double
doublechardouble
long doublelong doublelong double
long doublecharlong double
charcharchar

  

 Program to demonstrate implicit conversion.

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

float d, e;

clrscr();

a=10;

b=3;

c=a/b;                                    /*Line 10*/

printf(“\nc=%d”,c);

d=a/b;                                    /*Line 12*/

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

e=a/d;                                    /*Line 14*/

printf(“\ne=%f”,e);

c=a/d;                                    /*Line 16*/

printf(“\nc=%d”,c);

getch();

}

Output:

c=3

d=3.000000

e=3.333333

c=3

** In line 10 of above program, a and b are of int type so result of a/b will be of int type i.e. 3 which has been assigned to variable c which is also of int type. So c  will contain 3.

In line 12, a and b are of int type so result of a/b will be of int type i.e. 3. which has been assigned to variable d which is also of float type. So d will contain 3.000000.

In line 14, a is of int type and d is of float type so result of a/b will be of float type i.e. 3.333333 which has been assigned to variable e which is also of float type. So e will contain 3.333333.

In line 16, a is of int type and d is of float type so result of a/b will be of float type i.e. 3.333333 which has been assigned to variable c which is of int type. So c will contain 3 as int variable c can’t contain digits after decimal point.

  Explicit Conversion/Casting

This is also known as explicit conversion. It is the way to convert the Output: of an arithmetic expression into a data type specified by the programmer. It is basically used to change implicit conversion.

The syntax of casting is:

(Type) Expression

type refers to any valid data type of C language in which we want to convert the output of an expression.

Expression refers to an valid arithmetic expression of C language which is to be converted into a specific data type.

  

Program to demonstrate explicit conversion.

#include<stdio.h>

#include<conio.h>

main()

{

int a,b;

float c;

clrscr();

a=10;

b=3;

c=(float)a/b;         /*Line 10*/

printf(“\nc=%f”,c);

getch();

}

Output:

c=3.333333

** In line 10 of above program, a and b are of int type so result of a/b should be of int type i.e. 3 which has been assigned to variable c which is also of int type. So c will contain 3.

There are several kinds of explicit conversion.

Checked

Before the conversion is performed, a runtime check is done to see if the destination type can hold the source value. If not, an error condition is raised.

Unchecked

No check is performed. If the destination type cannot hold the source value, the result is undefined.

Bit pattern

The raw bit representation of the source is copied and it is re-interpreted according to the destination type.

TIPS and tricks

  • Bitwise operators can be used for encrypting or decrypting the data.
  • Bitwise AND operator is suitable for checking whether a particular bit is on or off.
  • Bitwise AND operator is suitable for turning off a particular bit in a number.
  • Bitwise OR operator is suitable for turning on a particular bit in a number.
  • A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integer operands and cannot be applied to float or double type values.
  • Casting should be used carefully as it may produce wrong results.

Test Your Knowledge

Q1. Define an operator and an operand.

Q2. What are various types of operators in C?

Q3. Explain unary operators of C.

Q4. Explain various binary operators of C.

Q5. Explain arithmetic operators of C.

Q6. Explain relational operators of C.

Q7. Explain logical operators of C.

Q8. Explain assignment/shorthand operators of C.

Q9. Explain bitwise operators of C.

Q10. Explain conditional operator giving a suitable example.

Q11. Explain sizeof() operator.

Q12. Explain implicit conversion in C.

Q13. Explain casting in C.

Q14 Define an expression. Explain its types.

Q15 Convert the following equations into corresponding C statements.

  1. a) Z= +
  2. b) c= + (a2 b2)
  3. c) +

 Q16  Write C programs for the following:

  • The basic salary of an employee is typed through the keyboard. DA is 45% of basic salary, and HRA is 10% of basic salary. Write a program to calculate his net salary calculated as basic salary+DA+HRA
  • Write a program to find the total marks and percentage of a student from five subjects. The marks of five subjects should be entered through the keyboard.
  • Write a program to interchange the values of two variables.
  • Write a program to find the circumference of a circle calculated as 2πr.
  • Write a program to find the area of triangle by using the formula. The value of s is calculated as

Q17 What do you understand by precedence and associativity of operators. Explain.

Q18 What would be the output of following expressions.

  1. 9 <10+5
  2. 2+6*4/2-3+2
  3. 10/5*3+2/2-3-3+2

Q19 What is the difference between prefix and postfix forms of increment operator.

Q20 Write the output of following programs;

  1. main()

{

printf(“%d”,(10/3));

}

  1. main()

{

int a=20;

printf(“%d”,a++);

printf(“%d”,++a);

 

}

  1. main()

{

int a=20;

printf(“%d”,a++   +   ++a);

}

  1. main()

{

int a=20;

printf(“%f”,(10/3));

printf(“%d”,(a%3));

}

  1. main()

{

int a=10;

printf(“%d”,(a+=5));

printf(“%d”,a–);

printf(“%d”,a);

}

f)

main()

{

int z=2*5+2*8;
printf(“%d”,z);

}

  1. main()

{

printf(“%d”,11 ^ 5);

}

  1. main()

{

int a=4,b;
b=a++ + ++a;
printf(“%d,%d,%d,%d”,b,a++,a,++a);

}

  1. main()

{

int z,x=2,y=-6,a=1,b=2;
z=x++ – –y*b/a;

printf(“%d”,z);

}

  1. main()

{

int a = 10;
float b = 6;
printf ( “%f” , a / b ) ;

}

  1. main()

{

int j;
j= 10*50% 3/2;

printf ( “%d” ,j) ;

}

 

  1. main()

{

printf(“%d  %d ”,sizeof(int), sizeof(float));
}

  1. main()

{

int i , j, k;
i=10;
j =20;
k =10;
k =!(i==j && i==k);

printf(“k=%d ”,k);
}

Back to: C Programming Language