Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Operators in C

The operators are types of symbols that inform a compiler for performing some specific logical or mathematical functions. The operators serve as the foundations of the programming languages. Thus, the overall functionalities of the C programming language remain incomplete if we do not use operators. In simpler words, the operators are symbols that help users perform specific operations and computations- both logical and mathematical on the operands. Thus, the operators operate on the available operands in a program.

In this article, we will take a closer look at Operators in C according to the GATE Syllabus for CSE (Computer Science Engineering). Read ahead to know more.

Table of Contents

Use of Operators in C

The operators basically serve as symbols that operate on any value or variable. We use it for performing various operations- such as logical, arithmetic, relational, and many more. A programmer must use various operators for performing certain types of mathematical operations. Thus, the primary purpose of the operators is to perform various logical and mathematical calculations.

The programming languages like C come with some built-in functions that are rich in nature. The use of these operators is vast. These operators act as very powerful and useful features of all the programming languages, and the functionality of these languages is pretty much useless without these. These make it very easy for programmers to write the code very easily and efficiently.

Types of Operators in C

Various types of operators are available in the C language that helps a programmer in performing different types of operations. We can handle different operations in a program using these types of operators:

  • Relational Operators
  • Arithmetic Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators
  • Misc Operators

Relational Operators

We use the relational operators in c when we would like to compare the values available for two given operands. Here is a table that states all the relational operators available in the C language, along with their individual functions.

Function of Operator Operator Example
To check if two operands are equal to each other. == 5 == 3 would return to 0

and 4 == 4 would return to 1

To check if two operands are unequal to each other. != 8 != 5 would return to 1

6 != 6 would return to 0

To check if the operand value on the left is comparatively greater than that of the operand value on the right. > 5 > 8 would return to 0

9 > 7 would return to 1

To check if the operand value on the left is comparatively smaller than that of the operand value on the right. < 2 < 0 would return to 0

4 < 8 would return to 1

To check if the operand value on the left is equal to or comparatively greater than that of the operand value on the right. >= 1 >= 5 would return to be 0

4 >= 2 would return to be 1

9 >= 9 would return to be 1

To check if the operand value on the left is equal to or comparatively smaller than that of the operand value on the right. <= 3 <= 6 would return to be 1

7 <= 3 would return to be 0

2 <= 2 would return to be 1

Note – Here, we get 1 as output when the condition is true, and 0 as output when the condition is false.

Arithmetic Operators

The arithmetic operators in C language help a user perform the mathematical operations as well as the arithmetic operations in a program, such as subtraction (-), addition (+), division (/), multiplication (*), the remainder of division (%), decrement (–), increment (++).

The arithmetic operators are of two major types:

  • Binary Operators – It works using two of the operators, such as -, +, /, *.
  • Unary Operators In C It works by making use of just a single operand (value), such as — and ++.
  • Also, Explore Ternary Operator in C.

Binary Operators

Here is a table that states all the binary arithmetic operators available in the C language, along with their individual functions. If P = 50 and Q = 25, then:

Function of Operator Operator Example
To subtract the value of the second operator from the first one. P – Q = 25
To add the value of the second operator with the first one. + P + Q = 75
To divide the value of the numerator with the value of the denominator. / P / Q = 2
To multiply the value of the second operator with the first one. * P * Q = 1250

Unary Operators

Here is a table that states all the unary arithmetic operators available in the C language, along with their individual functions. These are increment and decrement operators. If P = 50 and Q = 25, then:

Function of Operator Operator Example
To increase the value of the integer/ operator by 1. Decrement Operator P — = 49

and Q — = 24

To decrease the value of the integer/ operator by 1. Increment Operator P ++ = 51

and Q ++ = 26

Prefix and Postfix Increment/ Decrement Operators

The decrement operators and increment operators are of two major types:

Prefix – When we use the operator before the available variable in a program as a prefix, it is known as a prefix operator. In a prefix increment operator, the program first adds 1 and then assigns the resultant value to the variable. While in a prefix decrement operator, the program first subtracts 1 and then assigns the resultant value to the variable. For example, ++a and –a.

Postfix – When we use the operator after the available variable in a program as a postfix, it is known as a postfix operator. In a postfix increment operator, the program first assigns the value to the variable, then adds 1, and then assigns the resultant value. While in a postfix decrement operator, the program first assigns the value to the variable, then subtracts 1, and then assigns the resultant value. For example, a++ and a–.

Here is a table that states all the prefix and postfix unary arithmetic operators available in the C language, along with their individual functions. These are increment and decrement operators.

Type of Operator Sample Operator Expression Description/ Explanation
Prefix Increment Operator ++p p increases by a value of 1, then the program uses the value of p.
Postfix Increment Operator p++ The program uses the current value of p and increases the value of p by 1.
Prefix Decrement Operator –p p decreases by a value of 1, then the program uses the value of p.
Postfix Decrement Operator p– The program uses the current value of p and decreases the value of p by 1.

Logical Operators

The logical operators in c help a user determine if the results would be true or false. Here is a table that states all the logical operators available in the C language, along with their individual functions. If P = 1 and Q = 0, then:

Name of Operator Operator Description of Operator Example
Logical NOT ! We use this operator for reversing the available logical state of the operand. In case the condition turns out to be true, then this operator will make the value false. !(P && Q) turns out to be true.
Logical OR || The given condition turns out to be true if any of the operands happen to be non-zero. (P || Q) turns out to be true.
Logical AND && The given condition turns out to be true if only both the operands happen to be non-zero. (P && Q) turns out to be false.

Assignment Operators

We use the assignment operators in c for assigning any value to the given variable. Here is a table that states all the logical operators available in the C language, along with their individual functions.

Function of Operator Operator Example
To assign the values from the right operand to the left operand. = p = q
To add the value of the right operand with the value of the left operand. After that, assign the resultant value to the left one. += p += q is similar to that of p = p=q
To subtract the value of the right operand from the value of the left operand. After that, assign the resultant value to the left one. -= p -= q is similar to that of p = p-q
To multiply the value of the right operand with the value of the left operand. After that, assign the resultant value to the left one. *= p *= q is similar to that of p = p*q
To divide the value of the right operand with the value of the left operand. After that, assign the resultant value to the left one. /= p /= q is similar to that of p = p/q
To calculate the modulus of the values of the right operand and the left operand. After that, assign the resultant value to the left one. %= p %= q is similar to that of p = p%q

Bitwise Operators

We use bitwise operators in c for performing the operations of bit-level on various operands. It first converts the operators to bit-level, and after that, it performs various calculations.

The Bitwise operators basically work on the bits, and we perform bit-by-bit operations using them. Here is the truth table for the ^, &, and | here:

a b a & b a | b a ^ b
1 1 1 1 0
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1

If we assume that P = 60 and Q = 13 using the binary format, then it will appear as follows:

P = 0011 1100

Q = 0000 1101

—————–

P ^ Q = 0011 0001

P | Q = 0011 1101

P & Q = 0000 1100

~ P = 1100 0011

Here is a table that states all the bitwise operators available in the C language, along with their individual functions. If we assume that the value of variable P = 60 and variable Q = 13, then:

Name of Operator Description of Operator Operator Example
Binary AND & This operator copies the bits to the result only if they exist in both of the operands. (P & Q) = 12,

The binary value is 0000 1100

Binary OR | This operator copies the bits only if they exist in either of the operands. (P | Q) = 61,

The binary value is 0011 1101

Binary XOR ^ This operator copies the bits only if they are set in one of the operands but not in both of them. (P ^ Q) = 49,

The binary value is 0011 0001

Binary One’s Complement ~ This operator is unary. Also, it performs the effect of ‘flipping’ the available bits. (~P ) = ~(60),

The binary value is -0111101

Binary Left Shift << It shifts the value of the left operand to the left on the basis of the number of bits that the right operand specifies. P << 2 = 240,

The binary value is 1111 0000

Binary Right Shift >> It shifts the value of the left operand to the right on the basis of the number of bits that the right operand specifies. P >> 2 = 15,

The binary value is 0000 1111

Misc Operators

Apart from all the operators that we have discussed above, the C programming language uses a few other crucial operators, that include operators, (such as ? : and sizeof). Here is a table that states all the misc operators available in the C language, along with their individual functions.

Description of Operator Operator Example
To return the actual size of any given variable. Size of() If z is an integer, then the size of(z) will return to be 4.
To return the address of any given variable. & For instance, &z; would return the actual address for any given variable.
Pointer to any variable. * *z;
Conditional Expression. ? : When the Condition is true ? then the value X :

In any other case, the value Y

Precedence of Operators in C

The precedence of the operators in C language helps in determining the preference in which the operators must be evaluated in a program. Some operators display higher precedence as compared to the others in the program. For instance, the precedence of the multiplication operator here is much higher than that of the addition operator. Explore, Operator Precedence and Associativity in C to know more.

Let us consider an example where a = 5 + 2 * 7; so here, the variable a gets assigned with the value 19 and not 70. It is because the precedence of the multiplication operator (*) is higher than that of the addition operator (+). Thus, the program would first multiply 2 with 7, produce 14, and then add it with 5 to get 19 as the final result.

The table below displays the operators according to their precedence. The ones with higher precedence appear at the very top, and the ones with lower precedence appear at the very bottom. In any given expression, the evaluation of the operators with higher precedence occurs first.

Type of Operator Associativity Category
() [] -> . ++ – – Left to right Postfix
– + ! ~ – – ++ (type)* & sizeof Right to left The Unary Operator
/ * % Left to right The Multiplicative Operator
– + Left to right The Additive Operator
>> << Left to right The Shift Operator
< > >= <= Left to right The Relational Operator
!= == Left to right The Equality Operator
& Left to right Bitwise AND
^ Left to right Bitwise XOR
| Left to right Bitwise OR
&& Left to right Logical AND
|| Left to right Logical OR
?: Right to left Conditional
= -= += /= *= %=>>= &= <<= |= ^= Right to left Assignment
, Left to right Comma

Also, Explore: Increment and Decrement Operators in C, Left Shift Operator in C & Modulus Operator in C

Practice Problems on Operators in C

1. Take a look at the following program:

#include <stdio.h>

int main()

{

int x;

x = 2;

printf( “%d\n”, x );

printf( “%d\n”, x++ );

printf( “%d\n\n”, x );

x = 2;

printf( “%d\n”, x );

printf( “%d\n”, ++x );

printf( “%d\n”, x );

return 0;

}

Output:

The output of the code mentioned above will produce a result as:

A.

2 2 3 2 3 3

B.

2 3 3 2 2 3

C.

3 2 3 3 3 2

D.

3 3 2 3 2 2

Answer – A.

2 2 3 2 3 3

2. Take a look at the following snippet of a code:

int a = 10;

int b = i++%5;

What will be the value of a and b after we execute the code?

A. a is 10, and b is 1.

B. a is 10, and b is 0.

C. a is 11, and b is 0.

D. a is 11, and b is 1.

Answer – C. a is 11, and b is 0.

3. Take a look at the following snippet of a code:

int a = 10;

int b = ++i%5;

What will be the value of a and b after we execute the code?

A. a is 10, and b is 1.

B. a is 10, and b is 0.

C. a is 11, and b is 0.

D. a is 11, and b is 1.

Answer – D. a is 11, and b is 1.


FAQs

Q1

Why do we use operators in the C programming language?

The operators basically serve as symbols that operate on any value or variable. We use it for performing various operations- such as logical, arithmetic, relational, and many more. A programmer must use various operators for performing certain types of mathematical operations. Thus, the primary purpose of the operators is to perform various logical and mathematical calculations.
The programming languages like C come with some built-in functions that are rich in nature. The use of these operators is vast. These operators act as very powerful and useful features of all the programming languages, and the functionality of these languages is pretty much useless without these. These make it very easy for programmers to write the code very easily and efficiently.

Q2

What is the difference between prefix and postfix operators in C?

The decrement operators and increment operators are of two major types:
Prefix – When we use the operator before the available variable in a program as a prefix, it is known as a prefix operator. In a prefix increment operator, the program first adds 1 and then assigns the resultant value to the variable. While in a prefix decrement operator, the program first subtracts 1 and then assigns the resultant value to the variable. For example, ++a and –a.
Postfix – When we use the operator after the available variable in a program as a postfix, it is known as a postfix operator. In a postfix increment operator, the program first assigns the value to the variable, then adds 1, and then assigns the resultant value. While in a postfix decrement operator, the program first assigns the value to the variable, then subtracts 1, and then assigns the resultant value. For example, a++ and a–.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus for CSE (Computer Science Engineering), GATE CSE Notes, GATE CSE Question Paper, and more.

Also Explore,

Comments

Leave a Comment

Your Mobile number and Email id will not be published.

*

*