Operators are the foundation of any programming language. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. For example, ‘+’ is an operator used for addition, as shown below: c = a + b;Here, ‘+’ is the operator known as the addition operator and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. The functionality of the C programming language is incomplete without the use of operators. C has many built-in operators and can be classified into 6 types:
The above operators have been discussed in detail: These operators are used to perform arithmetic/mathematical operations on operands. Examples: (+, -, *, /, %,++,–). Arithmetic operators are of two types: a) Unary Operators:Operators that operate or work with a single operand are unary operators. For example: Increment(++) and Decrement(–) Operators int val = 5; ++val; // 6b) Binary Operators:Operators that operate or work with two operands are binary operators. For example: Addition(+), Subtraction(-), multiplication(*), Division(/) operators int a = 7; int b = 2; cout<<a+b; // 92. Relational Operators:These are used for the comparison of the values of two operands. For example, checking if one operand is equal to the other operand or not, whether an operand is greater than the other operand or not, etc. Some of the relational operators are (==, >= , <= )(See this article for more reference). int a = 3; int b = 5; a < b; // operator to check if a is smaller than b3. Logical Operators:Logical Operators are used to combine two or more conditions/constraints or to complement the evaluation of the original condition in consideration. The result of the operation of a logical operator is a Boolean value either true or false. For example, the logical AND represented as the ‘&&’ operator in C returns true when both the conditions under consideration are satisfied. Otherwise, it returns false. Therefore, a && b returns true when both a and b are true (i.e. non-zero)(See this article for more reference). (4 != 5) && (4 < 5); // true4. Bitwise Operators:The Bitwise operators are used to perform bit-level operations on the operands. The operators are first converted to bit-level and then the calculation is performed on the operands. Mathematical operations such as addition, subtraction, multiplication, etc. can be performed at the bit level for faster processing. For example, the bitwise AND operator represented as ‘&’ in C takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1(True). int a = 5, b = 9; // a = 5(00000101), b = 9(00001001) cout << (a ^ b); // 00001100 cout <<(~a); // 111110105. Assignment Operators:Assignment operators are used to assign value to a variable. The left side operand of the assignment operator is a variable and the right side operand of the assignment operator is a value. The value on the right side must be of the same data type as the variable on the left side otherwise the compiler will raise an error. Different types of assignment operators are shown below: This is the simplest assignment operator. This operator is used to assign the value on the right to the variable on the left. b) “+=”This operator is the combination of the ‘+’ and ‘=’ operators. This operator first adds the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. c) “-=”This operator is a combination of ‘-‘ and ‘=’ operators. This operator first subtracts the value on the right from the current value of the variable on left and then assigns the result to the variable on the left. d) “*=”This operator is a combination of the ‘*’ and ‘=’ operators. This operator first multiplies the current value of the variable on left to the value on the right and then assigns the result to the variable on the left. e) “/=”This operator is a combination of the ‘/’ and ‘=’ operators. This operator first divides the current value of the variable on left by the value on the right and then assigns the result to the variable on the left. 6. Other Operators:Apart from the above operators, there are some other operators available in C used to perform some specific tasks. Some of them are discussed here: a. sizeof operator
b. Comma Operator
c. Conditional Operator
d. dot (.) and arrow (->) Operators
e. Cast Operator
f. &,* Operator
Below is the implementation of the above-mentioned operators:
OutputFollowing are the Arithmetic operators in C The value of a + b is 15 The value of a - b is 5 The value of a * b is 50 The value of a / b is 2 The value of a % b is 0 The value of a++ is 10 The value of a-- is 11 The value of ++a is 11 The value of --a is 10 Following are the comparison operators in C The value of a == b is 0 The value of a != b is 1 The value of a >= b is 1 The value of a <= b is 0 The value of a > b is 1 The value of a < b is 0 Following are the logical operators in C The value of this logical and operator ((a==b) && (a<b)) is:0 The value of this logical or operator ((a==b) || (a<b)) is:0 The value of this logical not operator (!(a==b)) is:1 Operator Precedence ChartThe below table describes the precedence order and associativity of operators in C. The precedence of the operator decreases from top to bottom.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above. |