Operators in C
Definition: It is operates on values or variable.
Types of operators:
- Arithmetic operators
- Relational operators
- Assignment operator
- Logical operators
- Bit wise operators
- comma operators
- size of operators
- Ternary operator
operators are used in all programs.in each and every line so one need to be keen about operators and their usage.
Arithmetic Operators: These are the common operators we see which are used to perform tasks like addition,subtraction,multiplication,
division,modulus e.t.c
Operator | usage | example | answer |
+ | add two values | int a=5;
int b=8;
int c=a+b; | c is 13 |
- | subtract two values | int a=8;
int b=5;
int c=a-b; | c is 3 |
* | multiply two values | int a=8;
int b=5;
int c=a*b; | c is 40
|
/ | divide two values
and gives quotient | int a=10;
int b=5;
int c=a/b; | c is 2 |
% | divide two values and give remainder | int a=2;
int b=3;
int c=a%b; | c is 2 |
ADDITION OPERATOR:
It adds two same datatype values,if two are of not same datatype this converts into a bigger datatype .this process is called type conversion and it will be discussed later.Even if it's value is assigned to a smaller datatype it's value is converted into a smaller datatype.
int a=10;
float b=5.0;
float c=a+b;
// the value of c is 15.0
SUBTRACTION OPERATOR:
It subtracts two same datatype values,if two are of not same datatype this converts into a bigger datatype.
if it's value is assigned to a smaller datatype it's value is converted into a smaller datatype.
int a=10;
float b=5.0;
float c=a-b;
// the value of c is 5.0
int c=a-b;
//value of c is 5
MULTIPLICATION:
It multiplies two same datatype values,if two are of not same datatype this converts into a bigger datatype.
if it's value is assigned to a smaller datatype it's value is converted into a smaller datatype.
int a=10;
float b=5.0;
float c=a*b;
// the value of c is 50.0
int d=a*b;
// value of c is 50
DIVISION:
It divides two same datatype values,if two are of not same datatype this converts into a bigger datatype.
if it's value is assigned to a smaller datatype it's value is converted into a smaller datatype.
int a=10;
float b=5.0;
float c=a/b;
// the value of c is 2.0
int d=a/b;
// value of c is 2
MODULUS:
It divides two same datatype values, and returns remainder.
int a=10;
float b=5.0;
float c=a%b;
// the value of c is 0.0
int d=a%b;
// value of c is 0
RELATIONAL OPERATORS:
These are operators which are used to show the relation between two values or variables.
the operators are
SYMBOL | operation | Return values |
> | if a is greater than b | True |
> | if a is less than b | False |
< | if a is less than b | True |
< | if a is greater than b | false |
== | if a is equal to b | True |
== | if a is not equal to b | False |
ASSIGNMENT OPERATOR:
It is used to assign a value to a variable.
syntax:
variable_name = value
COMPOUND ASSIGNMENT OPERATORS:
Used to give values to value to variable in a short hand.
Syntax:
Variable_name (operator)=value
Assignment | usage |
a+=b | a=a+b |
a*=b | a=a*b |
a/=b | a=a/b |
a-=b | a=a-b |
a%=b | a=a%b |
LOGICAL OPERATORS:
Used to join expressions .They return True and false. Operators are && ,||, !
symbol | meaning | Return value |
! | not | True if value is False
False if is True |
&& | and | True if both expressions are True
In all other cases it is False |
|| | or | False only if both expressions are False |
It can combine two relational expressions.
EXPRESSION | RETURN VALUE |
!1 | 0 |
1<2 && 2<3 | 1 |
1==a | 1 |
1<=2 && 2>=3 | 1 |
BIT WISE OPERATORS:
These operators work on bits.
Operators are <<,>>,&,|,^,~
Operator | Meaning |
>> left shift | shifts n bits left |
<< right shift | shifts n bits right |
& | and-ing on bits |
| | or-ring on bits |
~ | Bit wise complement |
^ | xor -ring bits of number |
2 & 3 is 10 (2 in binary) &11(3 in binary )is 10 which is 2
2 | 3 is is 10 | 11 is 11 which is 3 in decimal.
decimal | binary |
1 | 01 |
2 | 10 |
3 | 11 |
4 | 100 |
5 | 101 |
Binary it has only two values 0 and ad every number can be represented using zeroes and ones.
SIZE OF operator:
this returns number of bytes a variable occupies.
EX:
sizeof( variable name)
usage | value |
sizeof(int) | 32 bits
64 bits |