Operators in C
Operators are the foundation of any programming language. Operators in C is a special symbol that tells the compiler to perform specific mathematical or logical function. The data items on which those operators are applied are known as operands.
The following is the list of the operators:
- Arithmetic operator
- Relational operator
- Logical operator
- Bitwise operator
- Conditional operator
- Assignment operator
- Arithmetic operator: These are the operators used to perform mathematical/arithmetic operations on operands.
For Example:
Operator Description Example + Added two operands A+B - Subtracts second from the first A-B * Multiples both operands A*B / Divide numerator by Denominator B/A % Modulus Operator and remainder of after an integer division B%A - Relational operator: In this relational operator we can compare of the values of two operands.
Here we can show some Example of Relational Operator.Assume variable A holds 10 and variable B holds 20 thenOperator Description Example ==' Check the value of two operands are equal or not.If the value are equal then condition become true. (A==B) is not true != Check the value of two operands are equal or not.If the value are not equal then condition become true. (A!=B) is true > Check the value of left operand is greater than value of right operand.If yes then condition become true. (A > B) is not true < Check the value of left operand is less than value of right operand.If yes then condition become true. (A < B) is true >= Check the value of left operands is greater than or equal the value of right operand.If yes than condition become true. (A >= B) is not true <= Check the value of left operands is less than or equal the value of right operand.If yes than condition become true. (A<=B) is true - Logical operator: In this logical operator are used to combine two or more conditions to complement the evaluation of the original condition in consideration.The result of this operation is a boolean value either true or false.
For ExampleOperator Description Example && Logical AND operator. If both the operands are non-zero then condition become true. (A && B) is false || Logical OR operator.If both operands is non-zero then condition become true. (A || B) is true ! Logical NOT operator.It is used to reverse the logical state of operand.If condition is true then operator will make it false. !(A && B) is true - Bitwise operator: The Bitwise Operator are used to perform the operations on the data at the bit-level.It consists of two digits, either 0 or 1. We have different types of bitwise operators in C programming language.This is the list of bitwise operators :
Operator Meaning of the Operator & Bitwise AND operator | Bitwise OR operator ^ Bitwise XOR operator ~ One's complement operator << Left shift operator >> Right shift operator Let’s look at the truth table of the bitwise operator
X Y X&Y X|Y X^Y 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 1 - Conditional operator: Conditional operator are also called ternary operator is one of the operator which is used in the decision making process.Conditional operator returns the one value if condition is true and return another value is condition is false.
Syntax :-
The Conditional operator formvariable = Expression1 ? Expression2 : Expression3
The condition(Expression1) is true then we will execute the condition and then return the result of(Expression2) otherwise if condition (Expression1) is false then we will execute and the result of (Expression3).
- Assignment operator: In this Assignment operator are used to assigning value to a variable.The right side operand of the assignment operator is a value and the left side operand of the assignment operator is a variable.
Different types of assignment operators are shown below:
Operator Description Example = Those operator are used to assign the value on the right to the variable on the right. a=10; += Those operator first add the current value of the variable and then assigns the result to the variable. (a+=b) can be written as (a=a+b) -= Those operator first subtract the current value of the variable and the assigns the result to the variable. (a-=b) can be written as(a=a-b) *= Those operator first multiplies the current value of the variable and then assigns the result to the variable. (a*=b) can be written as (a = a*b) /= Those operator first divides the current value of the variable and then assigns the result to the variable. (a/=b) can be written as (a = a/b)
The precedence and associativity of operators in C are given below:
Operator | Description | Associativity |
---|---|---|
Postfix | ()[]-> . ++ -- | left to right |
Unary | + - ! ~ ++ --(type)*& size of | right to left |
Multiplicative | * / % | left to right |
Additive | + - | left to right |
Shift | < < > > | left to right |
Relational | < <= > >= | left to right |
Equality | = = ' | left to right |
Bitwise AND | & | left to right |
Bitwise XOR | ^ | left to right |
Bitwise OR | | | left to right |
Logical AND | && | left to right |
Logical OR | || | left to right |
Conditional | ?: | Right to left |
Assignment | - = * = / = > > = < < = ^ = | Right to left |
Comma | , | left to right |
Submit your review | |