Operators in C
Operators are symbols that tell the compiler to perform specific mathematical or logical manipulations.
Types of Operators
1. Unary and Binary Operators
Unary operators need only one operand to perform the task or operation.
Example :- i++, i--, +3 etc.
Here i is operand and ++ is Operators.
Binary operators required two operands to perform the operation.
Example :- a + b, 3 + 5, 3 % 5 etc.
Here a and b is two operands and + is operator.
2. Assignment Operator
The single equal (=) symbol is referred as assignment operator. Assignment operator is used to assign the result of an expression to a variable.
Example :- variable = expression
A = 10
3. Arithmetic Operators
Arithmetic Operators are used to performing mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/) and modulus (%).
Example :- a + b, a - b, a * b, a % b etc.
4. Relational operators
Relational operators are used to comparing two quantities or values. Example-
Symbol |
Operation |
Usage |
== |
Equal |
X == Y |
!= |
Not equal |
X != Y |
> |
Greater than |
X < Y |
< |
Less than |
X > Y |
>= |
Greater than or equal |
X >= Y |
<= |
less than or equal |
X <= Y |
5. Logical Operators
These operators take boolean values as input and return boolean values as output.
Note: In C any non-zero number is treated as true and 0 as false.
Symbol |
Operation |
Usage |
Explanation |
&& |
AND |
x && y |
Returns true if both x and y are true else returns false. |
|| |
OR |
x || y |
Returns false if neither x nor y is true else returns true. |
! |
NOT |
! x |
Unary operator. Returns true if x is false else returns false. |
6. Increment and Decrement operators
These are unary operators. Unary operators are the operators which require only one operand.
Symbol |
Operation |
Usage |
Explanation |
++ |
Post-increment |
x++ |
Increment x by 1 after using its value |
-- |
Post-decrement |
x-- |
Decrement x by 1 after using its value |
++ |
Pre-increment |
++x |
Increment x by 1 before using its value |
-- |
Pre-decrement |
--x |
Decrement x by 1 before using its value |
7. Conditional Operator
Conditional Operator is similar to if-else. It is denoted by the symbol “ ? : ” and is an unusual operator provided by C. This operator is also called as ternary operator.
Syntax : (Condition ? true_value : false_value);
Example : (A > 100 ? 0 : 1);
Note :- In above example, if A is greater than 100, then 0 is returned else 1 is returned.
8. sizeof() operator
The sizeof() is a unary operator that returns the size of data(no. of bytes) (int, float, variables, array etc).
Example :- x= sizeof (a);
y= sizeof (float);
9. Comma Operator
Comma operators are used to link related expressions together.
Example :- int a, c = 5, d;
10. Bit wise operators
Bitwise operators are used in C programming to perform bit-level operations.
Operators |
Meaning of operators |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise XOR |
~ |
Bitwise complement |
<< |
Shift left |
>> |
Shift right |
Type conversion :-
Type conversion in C is used to convert a variable from one data type to another data type, and after type casting compiler treats the variable as of the new data type.
Syntax : (type_name) expression;
Example : a = (float) 15/6;
Type conversion in c can be classified into the following two types:
1. Implicit Type Conversion
Implicit Type Conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion.
2. Explicit Type Conversion
The type conversion performed by the programmer by posing the data type of the expression of specific type is known as explicit type conversion.
Example :- x= (int) a+b*d;