Introduction
C has large number of operators all the computation in C language can be done through operator various categories of operators for example automatic operator relational operator logical operator assignment operator unary operator
Expression
an expression is combination of one or more operands and 0 or more operators to compute value
for eg: z=x+y; x, and y are operands and (+) plus sign is operators.
Operators
operators are special character which instructor the compiler or interpreter to perform certain operation on some operands. operator specifies the operation to be performed . An operand may be variable, a function reference or a constant. some operators requires two or more operands while other act upon only one operands . the operators which act upon a single operands are called unary operators , the operators embedded between the two operands are called binary operators and the operators that act on three operands are called ternary operators .
In C , the operators can be prefix, postfix and infix. Basically prefix and postfix operators work on single operands .Some operators are indicated before operand, they are called prefix operators . Operators which are indicated after the operands is called postfix operators. When operators are used in between two or more operands , these are known an infix operators .
Example: ++i; Prefix unary operator
i++; Postfix unary operators
a+b; Binary operators / infix operators
Different type of C operators shown below:
Type of operators Symbolic Representation
1.Arithmetic operators +, -, *, /, %
2.Relational operators <, >, <=, >=, ==, !=
3.Logical operators &&, ||, !
4.Assignment operators =
5.Conditional operators ?=
6.Unary operators ++,--,-,sizeof etc.
7.Bitwise operators &,^,|,<<,>>,~
8.other operators ,
1. Arithmetic operators
Arithmetic operators are used for mathematical calculations. The arithmetic operator can operate on any built-in data type i.e. integer , float, char.
there are five arithmetic operators.
Arithmetic operator purpose example
* Multiplication 4*5=20
/ Division 15/3=5
% Modulus 10%3=1
+ Addition 2+3=5
- Subtraction 15-5=10
( the operands used with any of the operators can be of integer , float, or char type except the modulus operator. the operator of (%) modulus operator must be of integer)
Arithmetic operators can be used on single operand or two or more operands
Program:
To illustrate the use of arithmetic operators
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c,d,e,f,g;
printf("Enter the value of a and b ");
scanf("%d%d",&a,&b);
c=a+b;
d=a-b;
e=a*b;
f=a/b;
g=a%b;
printf("sum=%d",c);
printf("\n Difference=%d",d);
printf("\n Multiplication =%d",e);
printf("\n Division=%d",f);
printf("\n Reminder=%d",g);
getch();
}
Output:Enter the value of a & b 10 5
Sum = 15
Difference = 5
Multiplication = 50
Division = 2
Reminder = 0
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int x=3, y=5;
char c='A';
printf("Result 1 =%d",(x+y)>5);
printf("\n Result 2=%d",c<'B');
printf("\n Result 3=%d",x==5);
getch();
}
Output:Result 1 = 1
Result 2 = 1
Result 3 = 0
Relational operators are basically used in decision making statements like if statement, while statement, do-while and for statement.