operators in c programming
operators are used in c programming to manipulate data and variables.
Operator is a symbol which is use to perform a specified task. operator tells the compiler to perform arithmetic or logical operations.
operator mainly divided into two categories.
1. binary operator
2. unary operator
1. binary operator :
An operator which required minimum 2 operands to perform a certain task is known as a binary operator.
operand 1 operator operand 2
2. unary operator :
An operator which required only single operand to perform a certain task is known as a unary operator.
types of operators :
1. arithmetic operators(+, -, *, /, %)
2. assignment operators(=, +=, -=, *=, /=, %=)
3. relational operators(==, !=, >, <, >=, <=)
4. logical operators(&&, ||, !)
5. increment and decrement operators(++, --)
6. conditional operator or ternary operators( ? , : )
7. bitwise operators(&, |, ^, ~, >>, <<)
8. special operators( sizeof () )
1. Arithmetic operators :
This operators are use to perform algebraic operations between two or more operands. it is binary operators.
there are following operators are used in arithmetic operations
+, -, *, /, %
int a=10, b=20, c=3;
2. Assignment operators :
This operator is used to assigning a value to particular operand.
this operator is a binary operator. it is also known as a shorthand operators.
there are following operators are used in assignment operators.
=, +=, -=, *=, /=, %=
3. Relational operators :
This operators defines the relation between two operands such as an operand is >= other operand.
this operator is used to compare One operand to another operand. it is also use to compare arithmetic or logical expressions. It returns either true or false, or one or zero.
following are relational operators
int x=10, y=20;
x x>=y 0
1.Logical AND(&&) :
It will returns one when all expressions are written one otherwise it will return zero.
int a=10, b=20;
a>=b && a<=20
If first expression return 0 then it can not evaluate other expression, if first expression return one then it will evaluate another expression.
2. Logical OR(||) :
This operator return one when either any expression return one otherwise is zero.
int x=10, y=20;
x>=y || x<=y
If 1 found in first expression then it does not evaluate other expression.
3. Logical NOT(!) :
This operator returns opposite value of a particular expressions.
Expressions return one then answer is zero, if expression return 0 then answer is 1.
int x=20, y=30;
5. Increment and Decrement operators :
++, -- is known as increment or decrement operators.
these operators are used when we want to increment or decrement value by 1.
it is also known as a prefix or postfix operators.
1. prefix :
++x first add 1 to operand and result be assigning to variable on left operand is known as a prefix operator.
output x=11, y=11
2. postfix :
x++ first assign the value to the variable on the left hand and then increment the operand is known as a postfix operator.
output x=10, y=11
This operator also used into decrease the value from the number.
6. Conditional or Ternary operators :
This operator is used to check conditions. it is also known as a ternary operator because it has required minimum 3 operands or expressions.
exp1 ? exp2 : exp3
int a, b=10, c=20;
If expression 1 return true then expression 2 will be evaluated otherwise expression 3 will be evaluated.
this is same as if, else statement.
7. Bitwise operators :
This operator is used to check bit by bit value. this operator is used where our programming required testing of bit level. these operators are not applicable for float and double data type.
following operators are used in bitwise operator.
& bitwise AND
| bitwise OR
^ exclusive OR
~ one's complement
<< left shift
>> right shift
1. bitwise AND (&) :
This operator checks with bit by bit if both bits values are 1 then it will return 1 otherwise 0.
2. bitwise OR (|) :
This operator check both bits if either bits has value 1 then it will return 1 otherwise 0.
3. exclusive OR (^) :
In this operator exact one bit is 1 then result is 1 otherwise result is 0.
if result is in odd position of 1 then it will return 1 otherwise 0.
4. one's complement :
This operator convert 1 bit to 0 and 0 bit to 1. it is also known as a unary operator.
5. left shift(<<) :
In left shift operator left bits are move out and zeros are inserted at the right side.
If we have binary of 5 is 0101 then it perform left shift as per follow.
0101 (binary of 5)
5<<1= 1010 (binary of 10)
in short this operator perform multiply by 2 if 1 bit is shifted to the left.
6. right shift(>>) :
In right shift operator right bits are moved out and 0 are inserted at the left side.
this operator is used to perform division operations by 2 if 1 bit is shifted to the right.
following example shows how to perform right shift operations.
1010 (binary of 10)
10>>1= 0101 (binary of 5)
8. Special operator :
Following operators are known as special operator.
1. sizeof :
This operators find out number of bytes occupy by the variable.
int a; printf("%d",sizeof(a)); output 2 bytes
float b; printf("%d",sizeof(b)); output 4 bytes
long double x; printf("%d",sizeof(x)); output 10 bytes
2. comma(,) :
This operator is used to define more than one variable in a single statement.