C Operators

In this tutorial, you will learn about different C operators with the help of examples.


An operator is a symbol which operates on variables or a value. For example + is an operator to perform addition operation.

There are many operators available in C programming language to perform various operations.

Operators in C language can be broadly classified into the below categories.

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

1) C Arithmetic Operators

An arithmetic variable performs arithmetic operation such as addition, subtraction, multiplication, division, etc on numerical variables or numerical constants.

Suppose, variable A holds 10 and B holds 20 then:

Operator Meaning of Operator Example
+ Adds two operands. A + B = 30
Subtracts 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 remainder of after an integer division. B % A = 0

2) C Relational Operators

A relational operator checks relation between two operands. If the relation is true then it returns 1; if the relation is false, it returns 0.

Suppose, variable A holds 10 and B holds 20 then:

Operator Meaning of Operator Example
== Checks if the values of two operands are equal or not. If yes, then the condition becomes true. (10 == 20) evaluate to 0
!= Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. (10 != 20) evaluate to 1
> Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. (10 > 20) evaluate to 0
< Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. (10 < 20) evaluate to 1
>= Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. (10 >= 20) evaluate to 0
<= Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. (10 <= 20) evaluate to 1

3) C Logical Operators

The work of the logical operator is to return 0 or 1 depending upon the result of the expression. If the expression evaluate to true then it returns 1 and if the expression evaluate to 0, then it returns 0.

Logical operators are commonly used in C programming language for decision making.

Suppose, variable A holds 10 and B holds 20 then:

Operator Meaning of Operator Example
&& Logical AND. True only if all operands are true expression ((A==10) && (B>25)) evaluated to 0
|| Logical OR. True only if either one operand is true expression ((A==10) || (B>25)) evaluated to 1
! Logical NOT. True only if the operand is 0 expression !(A==10) evaluated to 0

4) C Bitwise Operators

Bitwise operators are used to perform bit level operations in C programming language. These operators are mainly used to perform numerical calculation faster.

Operator Meaning of Operator
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
~ Bitwise complement
<< Shift left
>> Shift right

5) C Assignment Operators

An assignment operator is used to assign a value to a variable. The most commonly used assignment variable is =.

Following are the commonly used assignment variable in C programming language.

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
<<= C <<= b C = C << b
>>= C >>= b C = C >> b
&= C &= b C = C & b
^= C ^= b C ^= b
|= C |= b C = C | b

6) C Misc Operators ( sizeof() and ternary )

Apart from the above operations, C also have few operators such as sizeof() and ternary ? operators.

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

 

Please get connected & share!

Categories

Advertisement