An operator is used to tells the compiler to perform specific mathematical or logical manipulation. Operators are used in the program to manipulate data and variables. The operator needs operand. An operand is a data item on which an operator acts. The operand may be one or more than one. At least one operand is required.
C has a large number of an operator that can be categorized in following groups.
- Arithmetic Operators
- Relational Operator
- Logical Operators
- Comma Operator
- Bitwise Operators
- sizeof Operators
- Increment and Decrement Operators
- Assignment Operators
- Conditional Operators
1. Arithmetic Operators
Arithmetic operators are used perform arithmetic operations. They are of two types are as follows
A. Unary Arithmetic Operator
B. Binary Arithmetic Operator
A. Unary Arithmetic Operator
Unary operator name suggests itself it require only one operand to operate.
e.g. +y, -x
It changes the sign of the operand
B. Binary Arithmetic Operator
There are 5 types of Binary Arithmetic Operators. It name suggest that it require 2 operands to be operated.
Operator | Purpose |
---|---|
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division |
% | Gives the remainder after division |
Note that the %(modulus operator) cannot be applied on floating point operands. Let us see one example how these operators are used. This example is already discussed in Basic C Programs Part: 1 let us the same example.
#include<stdio.h> main() { float a,b; printf("Enter value of a and b\n"); scanf("%f %f",&a,&b); printf("Addition is=%f\n",a+b); //Print result of addition printf("Subtraction is=%f\n",a-b); //Print result of subtraction printf("Multiplication is=%f\n",a*b); //Print result of multiplication printf("Division is=%f\n",a/b); //Print result of division return 0; }
The above example shows how Binary Arithmetic Operators are used. The output for the above program is
2. Relational Operators
Relational operators are used to comparing the values of two operators. These operators compare two values and result in one of two possible outcomes either true or false. It can compare character values or numeric values. Relational operators are as below.
This Relational operator always returns a value 0 or 1 only. Let us consider following program for better understanding.
Operator | Meaning |
---|---|
< | Less than |
<= | Less than or equal to |
== | Equal to |
!= | Not equal to |
> | Greater than |
>= | Greater than or equal to |
#include <stdio.h> main() { int a=5,b=7,c=10,e=5; printf("%d<%d=%d,%d<%d=%d\n",a,b,a<b,c,e,c<e); printf("%d<=%d=%d,%d<=%d=%d\n",a,b,a<=b,c,e,c<=e); printf("%d==%d=%d,%d==%d=%d\n",a,e,a==e,c,b,c==b); printf("%d!=%d=%d,%d!=%d=%d\n",a,e,a!=e,c,b,c!=b); printf("%d>%d=%d,%d>%d=%d\n",a,b,a>b,c,e,c>e); printf("%d>=%d=%d,%d>=%d=%d\n",a,e,a>=e,c,b,c>=b); return 0; }
3. Logical Operators
Logical operators combine two or more logical expressions and give 0 or 1 depending on the result is true or false. If the result is true it returns 1 and if the result is false it returns 0. There are 3 types of logical operators as
Operator | Name |
---|---|
&& | AND |
|| | OR |
! | Not |
The truth table for And, Or and Not operator
Let us consider the following C program for better understanding.
Expression 1 | Expression 2 | && (Result) | || (Result) |
---|---|---|---|
True(1) | True(1) | True(1) | True(1) |
True(1) | False(0) | False(0) | True(1) |
False(0) | True(0) | False(0) | True(1) |
False(0) | False(0) | False(0) | False(0) |
Expression | !(Result) |
---|---|
True(1) | False(0) |
False(0) | True(1) |
#include<stdio.h> main() { int a=5,b=7,c=10,e=5,result; result=(a<b)||(a>e); printf("Result for (a<b) || (a>e) is=%d\n",result); result=(a>b)||(a<e); printf("Result for (a>b) || (a<e) is=%d\n",result); result=(a!=5)||(a<c); printf("Result for (a!=5) || (a<C) is=%d\n",result); result=(a<b)&&(a>e); printf("Result for (a<b) && (a>e) is=%d\n",result); result=(a>b)&&(a<e); printf("Result for (a>b) && (a<e) is=%d\n",result); result=!(a==e); printf("Result for !(a==e) is=%d\n",result); result=!(a!=e); printf("Result for !(a!=e); is=%d\n",result); return 0; }
The output for above program is
4. Comma Operator
The comma operator(,) is used to permit different expressions to appear in situation where only one expression would be used. The expressions are separated by comma operator. This operator is Binary Operator. The comma operator has the lowest precedence of any C operator, and acts as a sequence point. The comma operator helps make code more compact. Let us see one program for comma operator.
#include <stdio.h> main() { int x=8, y=5, z=10, sum; sum=(x, y, z, x+y+z); printf("Sum=%d\n",sum); return 0; }
This comment has been removed by a blog administrator.
ReplyDelete