Types of Operators in C

Types of Operators in C

  1. Arithmetic Operator
  2. Increment/Decrement Operator
  3. Assignment Operator
  4. Logical Operator
  5. Bitwise Operator
  6. Misc Operator

Arithmetic Operator With Example

Arithmetic Operators are the operators which are used to perform mathematical calculations like addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). It performs all the operations on numerical values (constants and variables).

Increment/Decrement Operator With Example

C programming has basically two operators which can increment ++ and decrement -- the value of a variable. It can change the value of an operand (constant or variable) by 1. Increment and Decrement Operators are very useful operators that are generally used to minimize the calculation. These two operators are unary operators, which means they can only operate on a single operand. For example, ++x and x++ means x=x+1 or --x and x−− means x=x-1. 

There is a slight distinction between ++ or −− when written before or after any operand. 

If we use the operator as a pre-fix, it adds 1 to the operand, and the result is assigned to the variable on the left. Whereas, when it is used as a post-fix, it first assigns the value to the variable on the left i.e., it first returns the original value, and then the operand is incremented by 1.

Assignment Operator With Example

An assignment operator is mainly responsible for assigning a value to a variable in a program. Assignment operators are applied to assign the result of an expression to a variable. This operator plays a crucial role in assigning the values to any variable. The most common assignment operator is =. 

Relational/Comparison Operator With Example

Relational operators are specifically used to compare two quantities or values in a program. It checks the relationship between two operands. If the given relation is true, it will return 1 and if the relation is false, then it will return 0. Relational operators are heavily used in decision-making and performing loop operations.

The table below shows all the relational operators supported by C. Here, we assume that the variable A holds 15 and the variable B holds the 25.

Operator

==  It is used to check if the values of the two operands are equal or not. If the values of the two operands are equal, then the condition becomes true. (A == B) is not true.

!=    It is used to check if the values of the two operands are equal or not. If the values are not equal, then the condition becomes true. (A != B) is true.

>    It is used to check if the value of left operand is greater than the value of right operand. If the left operand is greater, then the condition becomes true. (A > B) is not true.

<   It is used to check if the value of left operand is less than the value of right operand. If the left operand is lesser, then the condition becomes true. (A < B) is true.

>=  It is used to check if the value of left operand is greater than or equal to the value of right operand. If the value of the left operand is greater than or equal to the value, then the condition becomes true. (A >= B) is not true.

<=   It is used to check if the value of left operand is less than or equal to the value of right operand. If the value of the left operand is less than or equal to the value, then the condition becomes true. (A <= B) is true.

Logical Operator With Example

In the C programming language, we have three logical operators when we need to test more than one condition to make decisions. These logical operators are: 

  • && (meaning logical AND)
  • || (meaning logical OR)  
  • ! (meaning logical NOT)

An expression containing a logical operator in C language returns either 0 or 1 depending upon the condition whether the expression results in true or false. Logical operators are generally used for decision-making in C programming.


Comments