My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. No Comments on Operators in C

An operator is a symbol that tells a computer to perform certain mathematical or logical operations. Operators are used in programs to manipulate data and variables. Operators are usually a part of the mathematical or logical expressions. Generally the usage of an operator is as shown below:

operator-syntax

In the above format, operand1 and operand2 can be either data or variables or expressions. Op is the operator. In C, based on the number of operands on which an operator can operate, the operators are divided into three types namely: unary, binary and ternary. Unary operators work on single operand, binary operators work on two operands and ternary operators work on three operands. In C, based on the functionality, operators are classified into 8 categories. They are:

  1. Arithmetic Operators
  2. Relational Operators
  3. Logical Operators
  4. Assignment Operators
  5. Increment and Decrement Operators
  6. Conditional Operators
  7. Bitwise Operators
  8. Special Operators

 

Arithmetic Operators

C provides all the basic arithmetic operators as shown below. The arithmetic operators can operate on any built-in data type in C. The unary minus operator multiplies its single operand with -1. Integer division truncates any fractional part. The modulo division operator (%) produces the remainder of an integer division. The modulo operator cannot be used on floating point values.

arithmetic-operators

 

Relational Operators

In C, whenever there is a need to compare two values and make a decision based on the outcome of the comparison, we use relational operators. For example, we can compare a person’s age with a value and based on whether the person’s age is less than or equal or greater than the value, we may take a decision. The relational operators in C are as shown below:

relational-operators

The relational operators are generally used in decision making statements like if, else if and in looping statements like for, while, do while etc. Relational operators always evaluates to 0 (false) or 1 (true).

 

Logical Operators

The relational operators are used to compare at most two values i.e. testing one condition. To test more than one condition, we use logical operators along with relational operators. The logical operators always evaluates to either 0 or 1 like relational operators. The logical operators available in C and their corresponding truth tables are as shown below:

logical-operators

 

Assignment Operators

The assignment operators are used to assign value of an expression to a variable. The general assignment operator is = (equal). In C, there are some special assignment operators known as shorthand operators. The syntax of shorthand operators is as shown below:

assign-op-ex

In the above shown syntax, var is a variable, op is an arithmetic operator and exp can be any expression or value or a variable. The use of shorthand operators has three advantages:

  1. Easier to write.
  2. The statement is more concise and easier to read.
  3. The statement is more efficient.

The assignment operators in C are as shown below:

assign-ops

 

Increment and Decrement Operators

The increment and decrement operators provided by C are used to increment or decrement the operand by a value of one. Both the increment and decrement operators are unary operators. These operators are used extensively inside for loop and while loop. There are two variations in how the increment and decrement operators can be used. They are as shown below:

inc-dec-ops

In the above shown syntax, var++ is known as post increment and ++var is known as pre increment. Although both of them increment the variable by a value of one, they behave differently when they are used in expressions. In expressions, when post increment is applied, the value of the variable is used in the evaluation of the expression and after the expression is evaluated, the value of the variable is incremented by a value of one. When pre increment is applied, the value of the variable is incremented by one first and then that value is used for evaluation of the expression.

The increment and decrement operators available in C are:

inc-dec-ops1

 

Conditional Operator

The conditional operator “? :” in C, is a ternary operator, which operates on three operands. This operator is used to construct conditional expressions of the form:

cond-op-syntax

In the above syntax, exp1, exp2 and exp3 refer to expressions. The “? :” works as follows: It evaluates the exp1 first and then based on the result of the exp1 it evaluates either exp2 or exp3. If the result of exp1 is true or non-zero, then exp2 is evaluated or if the result of exp1 is false or zero, then exp3 is evaluated.

 

Bitwise Operators

C supports a set of operators which operate at bit-level. These operators are known as bitwise operators. The bitwise operators are used for testing a bit, setting a bit, complementing a bit or for shifting the bits to left or right. The bitwise operators available in C are as shown below:

bitwise-ops

The truth table for the bitwise operators is as shown below:

bitwise-ops-truth-tables

Bitwise AND (&)

The bitwise AND operator is used to test whether a bit at particular position is zero or one. If it is one, it returns a non-zero value.

bitwise-and

In the above example, we are testing whether the bit at position 4 in the number 20 is set to one or not. Since it is zero, the result is zero.

Bitwise OR (|)

The bitwise OR operator is used to set a bit at particular position to one. Example usage of the bitwise OR operator is shown below:

bitwise-or

As you can see in the above example, we are trying to set the bit at position 4 in the number 20 to one. If the bit at the specified position is zero, the bitwise OR sets it to one. If it is already one, it leaves it as it is.

 

Bitwise Exclusive OR (^)

The bitwise Exclusive OR returns zero, if both the bits are equal. Otherwise, it returns one. Example usage of this operator can be seen below:

bitwise-exor

 

Bitwise NOT (~)

The bitwise NOT operator is used to complement every bit of the operand. If the bit is zero, it is set to one and if it is one, the bit is set to zero.

bitwise-not

 

Left Shift (<<)

The left shift operator is used to shift the bits in the operand to the left. The result of the left shift operation is equal to the operand multiplied by powers of two.

bitwise-left-shift

As seen in the above example, by left shifting the number 20 one time, we get the result as 40 which is 20 multiplied by 2. If we again left shift 20, we get the result 80 which is 20 multiplied by 4 and so on.

 

Right Shift (>>)

The right shift operator is used to shift the bits in the operand to the right. The result of the right shift operation is equal to the operand divided by powers of two.

bitwise-right-shift

As seen in the above example, by right shifting the number 20 one time, we get the result as 10, which is actually dividing 20 by 2. If we again left shift 20, we get the result as 5 which is 20 divided by 4 and so on.

 

Special Operators

C supports some special operators such as comma “,” operator, sizeof operator, address “&” operator, pointer operator “*” and some others.

 

Comma Operator

The comma “,” operator is used to combine multiple related expressions together. A comma separated list of expressions is evaluated from left to right and the value of the right most expression is the value of the combined expression.

 

sizeof Operator

The sizeof operator computes the size of an expression or variable or constant or a data type. This operator is used extensively for determining the length of an array, structure or union when the programmer does not know their actual sizes. The sizeof operator is also used in dynamic memory allocation. The general syntax of sizeof operator is as shown below:

sizeof-syntax

The operand can be either a value or variable or data type or an expression.

 

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me