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.
It is the variable, constant or expression on which an operator performs some calculation, comparison or some other manipulation.
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.
b. Unary Minus (-)
It is used to specify that the number is negative.
c. Pointer Operator (*)
It is known as pointer or pointer to operator. It is used to declare and use the pointer variable.
**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.
**Above statement stores the address of variable num in pointer variable ptr.
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
These operators are used for special purposes. There are many special operators provided by C language which are as follows:
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.
printf(“\nsizeof(long double)=%d”,sizeof(long double));
** 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.
** 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
|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|
|15||COMMA OPERATOR(,)||Left to Right|
It is the combination of operators and operands. Expression is of various types:
The expression which contains arithmetic operators is known as arithmetic expression.
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.
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.
9<=4 && 3<6
d. BITWISE expression:
The expression which contains bitwise operators is known as bitwise expression.
e. conditional expression:
The expression which contains conditional operator is known as conditional expression.
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
|short||long double||long double|
|int||long double||long double|
|long||long double||long double|
|float||long double||long double|
|double||long double||long double|
|long double||long double||long double|
|long double||char||long double|
Program to demonstrate implicit conversion.
float d, e;
c=a/b; /*Line 10*/
d=a/b; /*Line 12*/
e=a/d; /*Line 14*/
c=a/d; /*Line 16*/
** 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.
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 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.
c=(float)a/b; /*Line 10*/
** 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.
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.
No check is performed. If the destination type cannot hold the source value, the result is undefined.
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.
- a) Z= +
- b) c= + (a2 b2)
- 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.
- 9 <10+5
Q19 What is the difference between prefix and postfix forms of increment operator.
Q20 Write the output of following programs;
printf(“%d”,a++ + ++a);
printf(“%d”,11 ^ 5);
b=a++ + ++a;
z=x++ – –y*b/a;
int a = 10;
float b = 6;
printf ( “%f” , a / b ) ;
j= 10*50% 3/2;
printf ( “%d” ,j) ;
printf(“%d %d ”,sizeof(int), sizeof(float));
int i , j, k;
k =!(i==j && i==k);