Operators in C: Overview and Examples

Rumman Ansari   Software Engineer   2024-07-04 05:06:06   13573  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

C provides a rich operator environment. An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation. Operators are used in the program to manipulate data and variables. They usually form a part of the mathematical or logical expression.

C language operators can be divided into following categories:

  1. Arithmetic Operators
  2. Relational Operators
  3. Bitwise Operators
  4. Logical Operators
  5. Assignment Operators
  6. Conditional operator
  7. Misc Operators

Arithmetic Operators

  • The basic arithmetic operations in C Programming are addition, subtraction, multiplication, and division.
  • Arithmetic Operations are operated on Numeric Data Types as expected.
  • Arithmetic Operators are “Binary” Operators i.e they operate on two operands. These operators are used in mathematical expressions in the same way that they are used in algebra.

 

Operator Description Example
+ (Addition) Adds two operands 5 + 10 =15
- (Subtraction) Subtract second operands from first. Also used to Concatenate two strings 10 - 5 =5
* (Multiplication) Multiplies values on either side of the operator. 10 * 5 =50
/ (Division) Divides left-hand operand by right-hand operand. 10 / 5 =2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. 5 % 2 =1
++ (Increment) Increases the value of operand by 1. 2++ gives 3
-- (Decrement) Decreases the value of operand by 1. 3-- gives 2

 Examples of Arithmatic Operator


Relational Operators

  • Relational Operators are used to checking relation between two variables or numbers.
  • Relational Operators are Binary Operators.
  • Relational Operators returns “Boolean” value .i.e it will return true or false.
  • Most of the relational operators are used in “If statement” and inside Looping statement in order to check truthiness or falseness of condition.
Operators Descriptions Examples
== (equal to) This operator checks the value of two operands, if both are equal, then it returns true otherwise false. (2 == 3) is not true.
!= (not equal to) This operator checks the value of two operands, if both are not equal, then it returns true otherwise false. (4 != 5) is true.
> (greater than) This operator checks the value of two operands, if the left side of the operator is greater, then it returns true otherwise false. (5 > 56) is not true.
< (less than) This operator checks the value of two operands if the left side of the operator is less, then it returns true otherwise false. (2 < 5) is true.
>= (greater than or equal to) This operator checks the value of two operands if the left side of the operator is greater or equal, then it returns true otherwise false. (12 >= 45) is not true.
<= (less than or equal to) This operator checks the value of two operands if the left side of the operator is less or equal, then it returns true otherwise false. (43 <= 43) is true.

 Examples of Relational Operator


The Bitwise Operators

Java defines several bitwise operators, which can be applied to the integer types, long, int, short, char, and byte.
The following table lists the bitwise operators supported by C. Assume variable 'R' holds 60 and variable 'S' holds 13, then ?

Operator Description Example
& (bitwise and) Bitwise AND operator give true result if both operands are true. otherwise, it gives a false result. (R & S) will give 12 which is 0000 1100
| (bitwise or) Bitwise OR operator give true result if any of the operands is true. (R | S) will give 61 which is 0011 1101
^ (bitwise XOR) Bitwise Exclusive-OR Operator returns a true result if both the operands are different. otherwise, it returns a false result. (R ^ S) will give 49 which is 0011 0001
~ (bitwise compliment) Bitwise One's Complement Operator is unary Operator and it gives the result as an opposite bit. (~R ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< (left shift) Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. R << 2 will give 240 which is 1111 0000
>> (right shift) Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. R >> 2 will give 15 which is 1111

 Examples of Bitwise Operator


Logical Operators

Java supports following 3 logical operator.

Operator Description Example
&& (logical and)  If both the operands are non-zero, then the condition becomes true. (0 && 1) is false
|| (logical or)  If any of the two operands are non-zero, then the condition becomes true. (0 || 1) is true
(logical not) Logical NOT Operator Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(0 && 1) is true

 Examples of logical Operator


The Assignment Operators

Assignment operator supported by Java are as follows

operator description example
= assigns values from right side operands to left side operand A=B
+= adds right operand to the left operand and assign the result to left A+=B is same as A=A+B
-= subtracts right operand from the left operand and assign the result to left operand A-=B is same as A=A-B
*= mutiply left operand with the right operand and assign the result to left operand A*=B is same as A=A*B
/= divides left operand with the right operand and assign the result to left operand A/=B is same as A=A/B
%= calculate modulus using two operands and assign the result to left operand A%=B is same as A=A%B
<<= Left shift AND assignment operator. A <<= 2 is same as A = A<< 2
>>= Right shift AND assignment operator. A >>= 2 is same as A = A >> 2
&= Bitwise AND assignment operator. A &= 2 is same as A = A & 2
^= bitwise exclusive OR and assignment operator. A ^= 2 is same as A = A ^ 2
|= bitwise inclusive OR and assignment operator. A |= 2 is same as A = A | 2

 Examples of Assignment Operator


Miscellaneous Operators

There are few other operators supported by Java Language.

Conditional Operator / Ternary Operator

Java includes a one of the most special ternary (three-way) operator that can replace certain types of if-then-else statements. This operator is the . It can seem somewhat confusing at first, but the can be used very effectively once mastered. The has this general form:

 Expression1 ? Expression2 : Expression3 
 Expression ? value if true : value if false 

Here, Expression1 can be any expression(a>b) which evaluates to a boolean value (true or false). If Expression1 is true, then expression2 will work; otherwise, expression3 is evaluated. The result of the operation is that of the expression evaluated. Both Expression2 and Expression3 are required to return the same type, which can’t be void.

Example of conditional operator

 
#include

 void main() {
 int a, b;
 a = 20;
 b = (a == 1) ? 10: 25;
 printf( "Value of b is : %d\n",b ); 
 b = (a == 20) ? 20: 30; 
 printf( "Value of b is : %d\n",b ); 
   }
 

Output

 
Value of b is : 25
Value of b is : 20
Press any key to continue . . .
 

 Examples of Conditional Operator


 Examples of sizeof() Operator


MCQ Available

There are 116 MCQs available for this topic.

116 MCQTake Quiz