top

Search

C Tutorial

.

UpGrad

C Tutorial

Operators in C

Overview

How many types of operators in C are there? Why are operators in C important? The following article addresses all these questions and serves as a detailed tour guide to the main types of operators used in the C programming language. In addition to the arithmetic operators in C, it is important to understand the evaluation order of these operators. 

Importance of Operators in C programming language

Operators are essential components of any programming language, including C. They can be defined as specific symbols that enable you to manipulate and compute data in the C programming language. From data manipulation to performing logical and relational operations, they are entrusted with a lot of varied tasks. 

Operators are the building blocks of expression and decision-making in the C programming language that help developers generate efficient and expressive C code. 

 Arithmetic Operators

There are mainly six types of built-in operators in C. One such among them includes arithmetic operators. They are mainly used to perform mathematical calculations on numerical data types in C.

  • Addition Operator - Operators that add two operands and return their sum are referred to as addition operators. Example, int sum = num1 + num2;

  • Subtraction Operator - Operators that subtract the second operand from the first operand and return the difference are known as subtraction operators. Example, int difference = num1 - num2;

  • Multiplication Operator - They multiply two operands and generate the final product. Example: int product = num1 * num2;

  • Division Operator - They divide the first operand by the second operand and return the quotient. Example, int quotient = num1 / num2;

  • Modulus Operator - They divide the first operand by the second operand and return the remainder. Example, int remainder = num1 % num2;

  • Increment Operator - Operators that increase the value of an operand by one are referred to as increment operators. For example, num++;

  • Decrement Operator - Operators that decrease the value of an operand by two are known as decrement operators. Example, num--;

Relational Operators

Relational operators in C are primarily used to compare values and determine the relationships between operands. After evaluating the conditions thoroughly, they generate a result that can either be true or false. Overall, relational operators play a very crucial role in the decision-making process and comparing operations in C.

  • Equality Operator - They check whether the operands are equal. For example, if (num1 == num2).

  • Inequality Operator - They check if the operands are unequal. For example, if (num1 != num2).

  • Greater Than Operator - Operators that check if the left operand is greater than the right are referred to as greater than operators. For example, if (num1 > num2).

  • Less Than Operator - These refer to those operators that check if the left operand is less than the right operand. For example, if (num1 < num2).

  • Greater Than Or Equal To Operator - They determine if the left operand is greater than or equal to the right operand. For example, if (num1 >= num2).

  • Less Than Or Equal To Operator - They check if the left operand is less than or equal to the right operand. For example, if (num1 <= num2).

Logical Operators

Logical operators in C enable you to combine multiple conditions and then determine the overall truth value of the combined expression. You can use the same for conditional statements to thoroughly evaluate multiple conditions simultaneously and then make the necessary decision based on the combined results. 

  • Logical AND Operator - If both the left and right operands are true, it returns true. In any other case, it shall return false. Example, if (condition1 && condition2).

  • Logical OR Operator - When both or any one of two operands is true, it returns true. For example, if (condition1 || condition2) 

  • Logical NOT Operator - It reverses the truth value of the operand, meaning if the operand is true, it returns false, and vice versa. For example, if (!condition).

One interesting fact about logical operators is that you can use these alongside relational operators to generate more complex decision-making statements. 

Bitwise Operators

Bitwise operators in C refer to those operators that help to manipulate the individual bits of a binary number. There are primarily six bitwise operators used. They are, namely,

  • Bitwise AND Operator - It takes two numbers as operands and performs a Bitwise AND operation on them. If both the bits are 1, then the result generated will be 1; if not, then the result will be 0. 

  • Bitwise OR Operator - It performs OR operation on the corresponding bits of two operands. Even if one of the bits is 1, it will generate a result of 1, and if not, then the result will be 0.

  • Bitwise NOT Operator - It performs a bitwise complement, also referred to as NOT operation, on a single operand, inverting each bit. The 0s are flipped to 1s, and the 1s to 0s.

  • Bitwise XOR Operator - It performs bitwise exclusive OR operation on each bit of two numbers. Even if both the bits are different, it will generate a result of 1.

  • Left Shift Operator - It takes two numbers as operands; the left shifts the bit to the left by a certain number of positions as specified by the second operand. 

  • Right Shift Operator - It shifts the bits of the left operand to the right by a specified number of positions. If they are signed numbers, then the sign bits are replicated. If not, then zeros are used to fill in the left.

Assignment Operators

As the name suggests, the main responsibility of assignment operators in C is to assign values to variables. They do so by performing operations with arithmetic operators or bitwise operators and then assigning the result to the variables. Let's take a look at some of the most common types of assigned operators in C.

  • Simple Assignment Operator - The value on the right-hand side (p) is assigned to the variable on the left-hand side (q). For example, p = q

  • Addition Assignment Operator - The value on the right-hand side of the variable is added to the left-hand side, and the result is assigned to the variable. For example, p + = q.

  • Subtraction Assignment Operator - The value on the right-hand side is subtracted from the variable on the left-hand side, and the result is assigned to the variable. For example, p - = q.

  • Multiplication Assignment Operator - The product generated after multiplying the left-hand side of the variable by the right-hand side is equal to p. For example, p *= q

  • Division Assignment Operator - The value of the left-hand side variable is divided by the right-hand side, and the result is returned to the variable. For example, p / = q.

  • Modulus Assignment Operator - The remainder, after dividing p by q, is equal to p. For example, p % = q.

  • Bitwise AND Assignment Operator - Bitwise AND operation is conducted on both the variables and the result is returned to the variable. For example, p & = q. 

  • Bitwise OR Assignment Operator - Bitwise OR operation is performed on both variables, and the result is returned to the variable. For example, p | = q.

  • Bitwise XOR Assignment Operator - Bitwise XOR of p and q is equal to q. For example, p ^ = q.

  • Left Shift Assignment Operator - The bits of variables on the left-hand side are shifted to the left by the number of positions as specified by the right-hand side variable. The variable is then assigned the result. For example, p < < = q.

  • Right Shift Assignment Operator -The bits of the variable on the left-hand side are shifted to the right by the number of positions as specified by the right-hand side value. The variable is then assigned the result. For example, p > > = q. 

Ternary Operator

Ternary operators, also known as conditional operators, allow you to make decisions based on conditions. They evaluate the condition first and, if it is true, return the value of "expression1"; otherwise, they return the value of "expression2". The syntax for the ternary operator is as follows:

condition ? expression1 : expression2

The ternary operator is extremely useful in writing precise conditional expressions and assigning values based on conditions in a single code of line. However, you need to implement the same in the correct manner so as to avoid any overly complex expressions or other issues. 

Here is a small example that displays how to use the ternary operator in C.

#include <stdio.h>
int main() {
   int num = 7;
   // Check if num is even or odd using the ternary operator
   const char* result = (num % 2 == 0) ? "Even" : "Odd";
   printf("The number is %s\n", result);
   return 0;
}

Output 

The number is Odd

Here, we have used a variable named ‘num’ initialised with the value 7. We then used the ternary operator to check whether ‘num’ is odd or even. ‘(num % 2 == 0)’ checks if ‘num’ is divisible by 2 without a remainder, which means it is even. In case the condition is true, then the value ‘even’ is assigned to the variable ‘result.’ If not, then we assign the value ‘Odd.’ 

MISC Operator

Apart from these, there are also several other operators in C. Such include sizeof, &, and ‘*’, among others. These are referred to as MISC operators. 

  • sizeof - It returns the size of a variable. For example, if we have an integer a, using sizeof(a) will return 4.

  • & - It is used to return the variable address. For example, by using &i you can return the address of the variable.

  • * - It indicates the pointer to a variable. For example, if we have a variable i, using *i will indicate the pointer. 

Precedence and Associativity

Precedence and associativity rules are a crucial part of C, as they help to determine the order of evaluation of operators in an expression. For example, while operator precedence states which operators have high priority in the evaluation, operator associativity determines the exact order of evaluation when different operators of the same precedence exist in an expression.

Operator Precedence

Operators that have high precedence are usually the ones to get evaluated first. For example, the addition operator has lower precedence than the multiplication operator. Therefore, in an expression, ‘a + b * c’, ‘b * c” will be performed first, followed by ‘a + b’.

Operator Associativity 

Associativity helps determine the order of evaluation when multiple operators of the same precedence appear in an expression. For example, both addition and subtraction operators have the same precedence and left-to-right associativity. Therefore, in an expression, ‘a - b + c’, ‘a - b’ will be performed first, followed by ‘b + c’.

Conclusion

Summing up, operators in C are highly crucial since they serve a wide range of purposes. From data manipulation to increased efficiency, they enable you to build complex and logical expressions by combining variables and constants.  We hope that this article has provided you with all the essential information you need to know about operators in C. 

Additionally, you can also check out multiple courses that are available online. One such includes upGrad’s MS in Computer Science course offered under Liverpool John Moores University. It offers benefits such as AI-powered immersive learning experiences and hands-on experience to enhance your career prospects.

FAQs

Q1: Why do we use operators?

Operators in C are mainly used to manipulate individual data items, also known as operands, and generate the desired result. Special characters and keywords usually represent them.

Q2: What are the different types of operators in C?

The C programming language supports a wide variety of operators. Such includes arithmetic operators, relational operators, logical operators, and bitwise operators, among others. 

Q3: What do we mean by order of operations?

Order of operations basically states that those with higher precedence will be executed before operators with lower precedence. This means that additive operators will take precedence over relational operators. 

Leave a Reply

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