An operator is a symbol that specifies the mathematical, logical, or relational operations to be performed. C language supports different types of operators, which can be used with variable and constants to form an expression. These operators can be categorized in following major groups:
- Arithmetic Operators
- Logical Operators
- Relational Operators
- Equality Operators
- Unary Operators
- Conditional Operators
- Bitwise Operators
- Assignment Operators
- Comma Operators
- Sizeof Operators
Arithmetic operators can be applied to any integer or any floating point number. The addition, subtraction, multiplication and division (+, -, *, /) operators performs the arithmetic operation in C program.
This operator % is modulus operator is used to find the reminder of an integer division. This operator can be applied only to integer operand and cannot be used on float or double operand. Therefore, the code explains it in a better way:
printf(“Result is: %f”,c%5);
C supports three types of logical operators-
- Logical AND (&&)
- Logical OR (||)
- Logical NOT (!)
As in case of arithmetic expressions, logical expressions are evaluated from left to right.
- Logical AND (&&)
Logical AND operator is a binary operator, which simultaneously evaluates two values or relational expressions. If both or one of the operands is false, then the whole expression evaluates to false. The truth table of logical AND operators in given in table:Logical OR (||)
Logical OR returns a false value if both the operands are false. Otherwise it returns a true value. The truth table of logical OR operators in given in the table:
- Logical NOT (!)
The logical NOT operators takes a single expressions and negates the value of the expression. That is, logical NOT produce a 0 if the expression evaluates to a non-zero value and produce a zero.
A relational operator, also known as a comparison operator, in an operator that compares two values. Expressions that contain relational operators are called relational expressions.
Relational operators return true or false value, depending on whether the conditional relationship between the two operands holds or not.
Relational operators can be used to determine the relationship between the operands.
The relation operators are evaluated from left to right.
The operands of relational operators must evaluate to a number.
This is because arithmetic operators have a higher priority over relational operators.
int x = 10, y =30;
printf(“ %d %d %d ”, x, y, x<y );
C language supports two kinds of equality operators to compare their operands for strict equality or inequality. They are equal to (=) and not equal to (!=) operators. The equality operators have lower precedence than the relational operators.
The equal operator (==) returns true (1) if operands on both the sides of the operator have the same value; otherwise, it return =s false (0). On the contrary, the not-equal-to operators (!=) returns true (!) if the operands do not have the same value, else it returns false (0).
Unary operators act on single operands. C language supports there unary minus, increment, and decrement operators.
- Unary minus
- Increment and Decrement
The conditional operator or the ternary (?:) is just like an if-else statement that can be used within expressions. Such an operator is useful in situation in which there are two or more alternatives for an expression. The syntax of the conditional operator is
exp1 ? exp2 : exp3
Here, conditional operator is used in certain situations, replacing if-else condition phrases.
int a=10, b=8, c=5, Smallest;
Smallest = (a < b ? ( a < c ? a : c ) : ( b < c ? b : c ) );
As the name suggests, bitwise operators are those operators that perform operation at bit level. These operators include: bitwise AND, bitwise OR, bitwise XOR, and shift operators. The bitwise operators expect their operands to be integer and treat them as a sequence of bits.
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise NOT (!)
- Bitwise XOR (^)
In C, the assignment operators are responsible for assigning values to the variables. While the equal sign (=) is the fundamental assignment operators, C language also supports other assignment operators that provide shorthand ways to represent common variable assignments.
Here, it the compiler will assign 100 to the variable a.
The comma operator in C takes two operands. It works by evaluating the first and discarding its value, and then evaluates the second and returns the value as the result of the expression. Comma separated operands when chained together are evaluated in left-in-right sequence with the right most value yielding the result of the operator has the lowest precedence.
int a=1, b=3, c=0;
c= (++a, b+=a);
Now, the value of c is 6.
Sizeof operator is a unary operator used to calculate the variable type. This operator can be applied to all data types. This operator returns the size of variable.
unsigned int result_get;
Here, the result_get will be 2, which is the space required in the memory to store the integer type variable a.