Categories: C

Operators in C

Operators in C are symbols that are used to perform operations on operands(operands can be a constant, a variable, or a function on which operation is performed using the operators ). There can be many types of operations like arithmetic, logical, bitwise, etc.

The following types of operators are supported in the C language −

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operator
Arithmetic operators:

These operators are used to perform various arithmetic operations such as add, subtract, modulus, etc.

For instance, variable a holds 40 and variable b holds 20 then −

OperatorDescriptionExample
++Unary operator incrementing operand by 1++a = 41
Unary operator decrementing operand by 1–a = 19
+Adds two operandsa+b = 60
Subtract two operandsa-b = 20
*Multiply two operandsa*b = 800
/Divides numerator by de-numeratora/b = 2
%Modulus Operator used to calculate the remainder after divisionb%a = 20
Relational Operators:

Relational operators in C are also known as comparison operators. Assume variable a holds’ 40′ and variable b holds ’20’ then −

OperatorDescriptionExample
==if the values of two operands are equal then returns true otherwise false.(a == b) is false.
!=if the values of two operands are not equal then returns true otherwise false.(a != b) is true.
>if the value of left operand is greater than the value of right operand then returns true otherwise false.(a > b) is true.
<if the value of left operand is less than the value of right operand then returns true otherwise false.(a < b) is false.
>=if the value of left operand is greater than or equal to the value of right operand then returns true otherwise false.(a >= b) is true.
<=if the value of left operand is less than or equal to the value of right operand then returns true otherwise false.(a <= b) is false.
Logical Operators:

A logical operator in C is a symbol or word used to connect two or more expressions such that the value of the compound expression produced depends only on that of the original expressions and on the meaning of the operator.

OperatorDescriptionExample
&&Logical AND operator. If both the operands are non-zero, then returns true otherwise false.(a && b) is false.
||Logical OR Operator. If any of the two operands is non-zero, then returns true otherwise false.(a || b) is true.
!Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false.!(a && b) is true.
Bitwise Operators:

This operator works on bits and performs bit-by-bit operations. The truth tables for &, |, and ^ is as follows −

aba & ba | ba ^ b
00000
01011
11110
10011

Assume variable ‘a’ holds 60 and variable ‘b’ holds 13, then −

OperatorDescriptionExample
&Binary AND Operator returns 1 if both bits are 1 otherwise returns 0.(A & B) = 12, i.e., 0000 1100
|Binary OR Operator returns 1 if either bit is 1 otherwise 0.(A | B) = 61, i.e., 0011 1101
^Binary XOR Operator returns 1 if one of the bits is 1 otherwise 0.(A ^ B) = 49, i.e., 0011 0001
~Binary One’s Complement Operator is unary and has the effect of ‘flipping’ bits.(~A ) = ~(60), i.e,. -0111101
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.A << 2 = 240 i.e., 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.A >> 2 = 15 i.e., 0000 1111
Assignment Operators

The following table lists the assignment operators supported by the C language −

OperatorDescriptionExample
=Simple assignment operator. Assigns values from right side operands to left side operandC = A + B will assign the value of A + B to C
+=Add AND assignment operator. It adds the right operand to the left operand and assigns the result to the left operand.C += A is equivalent to C = C + A
-=Subtract AND assignment operator. It subtracts the right operand from the left operand and assigns the result to the left operand.C -= A is equivalent to C = C – A
*=Multiply AND assignment operator. It multiplies the right operand with the left operand and assigns the result to the left operand.C *= A is equivalent to C = C * A
/=Divide AND assignment operator. It divides the left operand with the right operand and assigns the result to the left operand.C /= A is equivalent to C = C / A
%=Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand.C %= A is equivalent to C = C % A
<<=Left shift AND assignment operator.C <<= 2 is same as C = C << 2
>>=Right shift AND assignment operator.C >>= 2 is same as C = C >> 2
&=Bitwise AND assignment operator.C &= 2 is same as C = C & 2
^=Bitwise exclusive OR and assignment operator.C ^= 2 is same as C = C ^ 2
|=Bitwise inclusive OR and assignment operator.C |= 2 is same as C = C | 2
Misc Operators ↦ sizeof & ternary:

Besides the operators discussed above, there are a few other important operators including sizeof and ? : supported by the C Language.

OperatorDescriptionExample
sizeof()Returns the size of a variable.sizeof(a), where a is an integer, will return 4.
&Returns the address of a variable.&a; returns the actual address of the variable.
*Pointer to a variable.*a;
? :Conditional Expression.If Condition is true ? then value X : Otherwise, value Y

note: also read about Variables & Constants in C & Data Types in C.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago