Table of contents
1.
Introduction
2.
Arithmetic Operators
2.1.
Example
3.
Relational Operators
3.1.
Example
4.
Bitwise Operators
4.1.
Example
5.
Logical Operator
5.1.
Example
6.
Assignment Operators
6.1.
Example
7.
Miscellaneous Operators
7.1.
Example
8.
Frequently Asked Questions
9.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to operators

Author RAJESH RUNIWAL
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

C operators are symbols that can be used to carry out mathematical or logical manipulations. The C programming language is rich with built-in operators. Operators help in a program for manipulating data and variables and form a part of the mathematical or logical expressions—types of operators in c programming

Also See, Sum of Digits in C, C Static Function

Arithmetic Operators

It is used to perform mathematical operations on operands. Some of the arithmetic operators are (+, -, *, /, %, ++, --).

Example

Assume variable A= 10 and variable B= 20 then −

Operator Description Example
+ Adds two operands. A + B = 30
- Subtracts the second operand from the first. A − B = -10
* Multiplies both operands. A * B = 200
/ Divides numerator by de-numerator. B / A = 2
% Modulus Operator and the remainder after an int division. B % A = 0
++ The increment operator increases the integer value by one. A++ = 11
-- The decrement operator decreases the integer value by one. A-- = 9

Relational Operators

This is used to compare two quantities or values.

Example

Assume variable A=20 and variable B= 30 then −

Operator Description Example
== Suppose the values of two operands are the same or no longer. If the solution is sure, then the situation will become true. (A == B) is not true.
!= Suppose the values of two operands are identical or no longer. If the values are not identical, then the situation becomes true. (A != B) is true.
> Checks suppose the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true. (A > B) is not true.
< Checks suppose the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true. (A < B) is true.
>= Checks suppose the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true. (A >= B) is not true.
<= Checks suppose the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true. (A <= B) is true.

Bitwise Operators

C provides a particular operator for bit operation between two variables.

Example

Assume variable A= 60 and variable B=13, then −

Operator Description Example
& Binary AND Operator (A & B) = 12, i.e., 0000 1100
| Binary OR Operator (A | B) = 61, i.e., 0011 1101
^ Binary XOR Operator (A ^ B) = 49, i.e., 0011 0001
~ Binary Ones Complement Operator (~A ) = ~(60), i.e,. -0111101
<< Binary Left Shift Operator A << 2 = 240 i.e., 1111 0000
>> Binary Right Shift Operator A >> 2 = 15 i.e., 0000 1111

Logical Operator

C provides three logical operators when we test more than one condition to make decisions. These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical NOT).

Example

Assume variable A=1 and variable B= 0 then −

Operator Description Example
&& I called the Logical AND Operator. Suppose both the operands are non-zero, then the condition becomes true. (A && B) is false.
|| I called the Logical OR Operator. Suppose any of the two operands are non-zero, then the condition becomes true. (A || B) is true.
! They called Logical NOT Operator. It's used to reverse the logical state of its operand. Suppose a condition is true, then Logical NOT the Operator will make it false. !(A && B) is true.


Also see, Tribonacci Series and  Short int in C Programming

Assignment Operators

This Operator is applied to assign the result of an expression to a variable. C has a collection of shorthand assignment operators.

Example

Operator Description Example
= Simple assignment operator. Assign values from right operands to left side operands

C = A + B will assign a value of A + B to C

 

+= Increments then assign

C += A equals C = C + A

 

-= Decrements then assign C -= A equals C = A - B
*= Multiplies then assign

C *= A equals C = C * A

 

/= Divides then assign

C /= A equals C = C / A

 

%= Modulus then assign C %= A equals C = C % A
<<= Left shift and assign C <<= 2 is same as C = C << 2
>>= Right shift and assign

C >>= 2 is same as C = C >> 2

 

&= Bitwise AND assign

C &= 2 is same as C = C & 2

 

^= Bitwise exclusive OR and assign C ^= 2 is same as C = C ^ 2
|= Bitwise inclusive OR and assign C |= 2 is same as C = C | 2

Miscellaneous Operators

Some other important operators, including sizeof and ? : It comes under this category:

Example

Operator Description Example
sizeof() Returns the size of a variable.

sizeof(a), where a is an int, will return 4.

 

& Returns the address of a variable. &a; returns the real address of the variable.
* Pointer to a variable. *a;
? : Conditional Expression. If Condition is true? then value X: Otherwise, value Y

 

Read about Bitwise Operators in C and Floyd's Triangle in C here.

Frequently Asked Questions

  1. How can we set a particular bit in C?
    We can use OR operator to set a particular bit in C. If we want to set ith bit, we can do so by Number  | =  (1<< ith Position).
     
  2. Why n++ executes faster than n+1?
    Ans. The expression n++ requires single machine instruction such as INR to carry out increment operation, whereas n+1 requires multiple instructions to carry out increment operation. So, the expression n++ gets executed faster than the expression n+1.
     
  3. What is the use of bitwise operators?
    Ans. Operating systems require manipulating data at addresses, which requires manipulating individual bits or groups of bits. For this purpose, bitwise operators are used. These operators are found in C, C++, and Java. Bitwise operators allow us to read and manipulate bits in variables of certain types.

Key Takeaways

We learned about all types of operators, explained all operators types, and learned about the use of operators where we can use all operators.

 It will become, in reality, simple to examine and use the code if the code is divided into functions. For more exercise, you can visit interview Experiences of top tech companies for practice.

Live masterclass