Operators in C Language
Operators in any computer language are the symbols that perform a specific task in the program and based on the number of operands they are classified into the following categories.
The C language consists of three types of operators – Unary, Binary, and Ternary operators.
Unary Operator
This type of operator required one operand or one argument.
A unary operator is further divided into sub-parts
- Unary Minus
This operator changes the sign of the argument provided to the operator. The negative argument changes to positive and the positive argument changes to negative.
Example
Int a = 25;
Int b = -a;
Value of b equal to b = -25 - Increment Operator
This operator is used to increase the value of the argument by one. Increment operators are of two types
- Prefix Increment
In this type of increment, the operator is followed by the argument i.e. ++a, and the value of the argument is changed before it is used.
Example
Int a = 15; ---> this point value of a is 15
int b = ++a --> this point value of b is 16 - Postfix Increment
In this type of increment, the argument is followed by the operator i.e. a++, and the value of the operator changes after it is used.
Example
Int a = 15; ---> this point value of a is 15
int b = a++; --> this point value of b is 15
int c=a; --> this point value of c is 16
- Prefix Increment
- Decrement Operator
This operator is used to decrease the value of the argument. Decrement operator is of two types
- Prefix decrement
In this type of decrement, the operator is followed by the argument i.e. --a, and the value of the argument is changed before it is used.
Example
Int a = 15; --> this point value of a is 15
int b = --a; --> this point value of b is 14 - Postfix decrement
In this type of decrement, the argument is followed by the operator i.e. a--, and the value of the operator changes after it is used.
Example
Int a = 15; --> this point value of a is 15
int b = a--; --> this point value of b is 15
int c=a; --> this point value of c is 14
- Prefix decrement
Binary Operator
Binary operator requires two operands.
Binary operators are further divided into the subcategory
- Binary Arithmetic Operator
These are the operators used to perform mathematical operations using two operands and provide the results.
- Addition
This operator adds two numbers and the syntax for this operator is ‘+’.
Example
Int a = 15;
int b = 10;
int c=a+b; --> value of c would be 25 - Subtraction
This operator subtracts the second operand from the first operand and the syntax for this operator is ‘-‘.
Example
Int a = 15;
int b = 10;
int c=a-b; --> value of c would be 5 - Multiplication
This operator multiplies the second operand from the first operand and the syntax for this operator is ‘*’.
Example
Int a = 15;
int b = 10;
int c=a*b; --> value of c would be 150 - Division
This operator divides the first operand from the second operand and the syntax for this operator is ‘/’.
Example
Int a = 15;
int b = 10;
int c=a/b; --> value of c would be 1 - Remainder or Modulo
This operator gives us the remainder when the first operand is divided by the second operand and the syntax for this operator is ‘%’.
Example
Int a = 15;
int b = 10;
int c=a%b; --> value of c would be 5
- Addition
Ternary Operator
For the execution of the ternary operator, the result of the binary condition is used. The binary condition act as the input which is then used as an if-else statement.
Syntax of ternart operator
binaryCondition ? valueReturnedIfTrue : valuReturnedIfFalse;
Example-
int findminimum(int a, int b){ Return (a<b) ? a : b; // If a < b it returns a and if a>b it return b }
Assignment Operator
This type of operator is used to assign the value to the variables. The assignment operator accepts a variable as the left operand and a value as the right operand.
"+=": The "+=" operator combines the "+" and "=" operators. This operator assigns the result to the variable on the left after adding the current value of the variable on the left to the value on the right.