Operators in C
Operator is a symbol which alerts compiler that mathematical, logical or relational operation is to be performed. Operator performs an operation on the operands.
Operands are the ones on which the operation is being performed by the operator
Types of Operators in C :-
Operands are the ones on which the operation is being performed by the operator
Types of Operators in C :-
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bit-wise Operators
- Misc Operators
Arithmetic Operators
There are three types of Arithmetical operations based on the number of operands the operators operates on.
I. Unary Arithmetical Operators : (+ , - , ++, --)
Unary operators is an operator which operates on only one operator. Unary operator '-' negates the value of its operand, (defines sign of operand, that it is negative) . Unless the '-' is given the number is positive , hence the '+' operator does not need to be used explicitly. The operator '++' increments the operand and the operator '--' decrements it. If '++' or '--' is used as a prefix to the operand then the operand is first incremented then it does its task. If these operators are used as a postfix to the operand then the operand is incremented after its task is done.
II. Binary Arithmetical Operators : (+,-,*,/,%)
Binary operator operates on two operators. These have all our basic arithmetic operators which are ' + ', ' - ', ' * '(for multiplication), ' / '(for division). The operator ' % ' which is called modulo operator is used to get remainder after the division of two integers.
I. Unary Arithmetical Operators : (+ , - , ++, --)
Unary operators is an operator which operates on only one operator. Unary operator '-' negates the value of its operand, (defines sign of operand, that it is negative) . Unless the '-' is given the number is positive , hence the '+' operator does not need to be used explicitly. The operator '++' increments the operand and the operator '--' decrements it. If '++' or '--' is used as a prefix to the operand then the operand is first incremented then it does its task. If these operators are used as a postfix to the operand then the operand is incremented after its task is done.
II. Binary Arithmetical Operators : (+,-,*,/,%)
Binary operator operates on two operators. These have all our basic arithmetic operators which are ' + ', ' - ', ' * '(for multiplication), ' / '(for division). The operator ' % ' which is called modulo operator is used to get remainder after the division of two integers.
Assignment Operators
I.Simple Assignment ( = ) :
Simple assignment is carried using a simple equal to ('=') operator. In this, the operand which is on the left side of this operator is assigned to the operand on the right side of the same type. e.g. a float cannot be assigned to an integer unless it is type casted.The left operator looses its value and will now hold the newly assigned value.
e.g. Suppose, there is an integer variable a. If we write a = 5, now the value of a is 5.
II.Compound Assignment(+=, -=, *=, /=, %=, &=, ^=, |= ) :
In compound assignment we do some operation on the left operand before assigning it a new value. We don't directly assign the value of right operand to left operand. In the compound operator there are two character a arithmetical operator and a simple assignment operator which together form the compound operator (e.g in '*=' operator there '+' which is arithmetic operator and '=' which is simple assignment operator which together form compound assignment operator '*='). So, the left operand is operated by the right operand with the arithmetical operator inside the compound operator and then that value is assigned to the left operand.
e.g. In a += 3, a is operated by 3 with '+' operator , so we add 3 to a and then a + 3 is assigned to a.
III.Expression Assignment :
Expression assignment is an assignment which has a mathematical expression as the right operand of a simple operator.
e.g. A = 5 + ((b = 8) + (c = 2)) - 4
Simple assignment is carried using a simple equal to ('=') operator. In this, the operand which is on the left side of this operator is assigned to the operand on the right side of the same type. e.g. a float cannot be assigned to an integer unless it is type casted.The left operator looses its value and will now hold the newly assigned value.
e.g. Suppose, there is an integer variable a. If we write a = 5, now the value of a is 5.
II.Compound Assignment(+=, -=, *=, /=, %=, &=, ^=, |= ) :
In compound assignment we do some operation on the left operand before assigning it a new value. We don't directly assign the value of right operand to left operand. In the compound operator there are two character a arithmetical operator and a simple assignment operator which together form the compound operator (e.g in '*=' operator there '+' which is arithmetic operator and '=' which is simple assignment operator which together form compound assignment operator '*='). So, the left operand is operated by the right operand with the arithmetical operator inside the compound operator and then that value is assigned to the left operand.
e.g. In a += 3, a is operated by 3 with '+' operator , so we add 3 to a and then a + 3 is assigned to a.
III.Expression Assignment :
Expression assignment is an assignment which has a mathematical expression as the right operand of a simple operator.
e.g. A = 5 + ((b = 8) + (c = 2)) - 4
Relational Operators
The relational operators are binary operators witch tells the relation between two operands. The relational operators are (<) less than, (>) greater than, (<=) less than or equal to, (>=) greater than or equal to, (==) is equal to, (!=) not equal to. The operator '==' is different than assignment operator '='. These relational operators are used to tell the relation between variables or number , etc. When we say a>b it returns the value '1' or '0'. If the relation is true, it will return 1 and if the relation is false then it will return 0.
Consider the following piece of code for example :-
int a = 1;
int b = 2;
int c = a > b;
printf(" Output : %d",c);
Output of this code will be :-
Output: 0
[It is obvious that this code is written in a function]
Consider the following piece of code for example :-
int a = 1;
int b = 2;
int c = a > b;
printf(" Output : %d",c);
Output of this code will be :-
Output: 0
[It is obvious that this code is written in a function]
Logical Operators
The logical operators are also binary operators (except 'NOT') which perform logical operations such as 'AND', 'OR' and 'NOT' on the operands and returns the integer value 1 or 0(1 stands for true and 0 stands for false).
Logical Operators in C are :-
1. AND :
Symbol : &&
Working : It returns 1 if both the operands on left and right are true (i.e. 1) ,else returns 0.
2. OR :
Symbol : ||
Working : It returns 1 if either of the operand is true(i.e. 1) , else returns 0.
3. NOT :
Symbol : !
Working : It is the only unary logical operator which returns 1 if operand is 0 and returns 0 if operand is 1.
Logical Operators in C are :-
1. AND :
Symbol : &&
Working : It returns 1 if both the operands on left and right are true (i.e. 1) ,else returns 0.
2. OR :
Symbol : ||
Working : It returns 1 if either of the operand is true(i.e. 1) , else returns 0.
3. NOT :
Symbol : !
Working : It is the only unary logical operator which returns 1 if operand is 0 and returns 0 if operand is 1.
Bit-Wise Operators
There are 6 bit-wise operators provided in C which are :
1. Bit-wise AND ( & )
2. Bit-wise OR ( | )
3. Bit-wise X-OR ( ^ )
4. Bit-wise Negation ( ~ )
5. Bit-wise Left Shift ( << )
6. Bit-wise Right Shift ( >> )
Bit-wise operators expects their operands to be integer quantities. They convert those integers to bits (i.e Decimal numbers/integers are converted to binary numbers). The operations are performed bitwise.
[click here to see decimal to binary conversion]
Bit-wise AND ( & ) :
Bit-wise AND sets the bit 1 to the result if it is present in both the operands or else the result bit is 0.
Bit-wise OR ( | ) :
Bit-wise AND sets the bit 1 to the result if it is present in either of the operands or else the result bit is 0.
Bit-wise X-OR ( ^) :
Bit-wise AND sets the bit 1 to the result if it is present in one of the operands but not both otherwise the result bit is 0.
Bit-wise Negation (~) :
Bit-wise negation change the bit value of the operand in the result. (i.e changes 1 to 0 and 0 to 1) .
Bit-wise Left Shift (<<) :
Bit-wise Left Shift operator shift the left operand bits by the number of bits specified by the right operand.
Bit-wise Right Shift (>>) :
Bit-wise Right Shift operator shifts the left operand bits by the number of bits specified by the right operand.
1. Bit-wise AND ( & )
2. Bit-wise OR ( | )
3. Bit-wise X-OR ( ^ )
4. Bit-wise Negation ( ~ )
5. Bit-wise Left Shift ( << )
6. Bit-wise Right Shift ( >> )
Bit-wise operators expects their operands to be integer quantities. They convert those integers to bits (i.e Decimal numbers/integers are converted to binary numbers). The operations are performed bitwise.
[click here to see decimal to binary conversion]
Bit-wise AND ( & ) :
Bit-wise AND sets the bit 1 to the result if it is present in both the operands or else the result bit is 0.
Bit-wise OR ( | ) :
Bit-wise AND sets the bit 1 to the result if it is present in either of the operands or else the result bit is 0.
Bit-wise X-OR ( ^) :
Bit-wise AND sets the bit 1 to the result if it is present in one of the operands but not both otherwise the result bit is 0.
Bit-wise Negation (~) :
Bit-wise negation change the bit value of the operand in the result. (i.e changes 1 to 0 and 0 to 1) .
Bit-wise Left Shift (<<) :
Bit-wise Left Shift operator shift the left operand bits by the number of bits specified by the right operand.
Bit-wise Right Shift (>>) :
Bit-wise Right Shift operator shifts the left operand bits by the number of bits specified by the right operand.
Misc Operators
There are some of the operators which to not fit in any type mentioned above, hence they are mentioned in Misc Operators. Different Misc operators and their operation is give below :
Conditional Operator ( ? :) :
It is ternary operator in C. It is used as if-else (e.g. if a > b then return c else return d is done using 'a>b?c:d' ).
Click here to know about if else statement which is later chapter.
Size of Operator ( sizeof() ) :
It return the size of the type of variable specified inside the parenthesis ( '(' and ')' ). It returns an unsigned integer type of at least 16 bit.
e.g. sizeof(int) will return 2 or 4 depending on the machine, since size of integer is generally 2 bytes in 16-bit machine and 4 bytes in 32-bit machine.
Comma Operator ( , ) :
It allows evaluation of multiple expressions, separated by comma, from left to right in order and evaluated value of the rightmost expression is accepted as the final result.
Address of Operator ( & ) :
It returns the address of a variable in terms of memory location. As the variables are stored in memory. The starting memory location of the memory where the variable is stored is returned by this operator.
( * ) Operator :
Used to declare pointer. Also it gives the value at the address specified.
Conditional Operator ( ? :) :
It is ternary operator in C. It is used as if-else (e.g. if a > b then return c else return d is done using 'a>b?c:d' ).
Click here to know about if else statement which is later chapter.
Size of Operator ( sizeof() ) :
It return the size of the type of variable specified inside the parenthesis ( '(' and ')' ). It returns an unsigned integer type of at least 16 bit.
e.g. sizeof(int) will return 2 or 4 depending on the machine, since size of integer is generally 2 bytes in 16-bit machine and 4 bytes in 32-bit machine.
Comma Operator ( , ) :
It allows evaluation of multiple expressions, separated by comma, from left to right in order and evaluated value of the rightmost expression is accepted as the final result.
Address of Operator ( & ) :
It returns the address of a variable in terms of memory location. As the variables are stored in memory. The starting memory location of the memory where the variable is stored is returned by this operator.
( * ) Operator :
Used to declare pointer. Also it gives the value at the address specified.