Operators in C | GATE Notes (2024)

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
  • Types Of Operators In C
  • Precedence Of Operators In C
  • Practice Problems On Operators In C
  • FAQs

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 OperatorOperatorExample
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, ExploreTernary 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 OperatorOperatorExample
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 OperatorOperatorExample
To increase the value of the integer/ operator by 1.Decrement OperatorP — = 49

and Q — = 24

To decrease the value of the integer/ operator by 1.Increment OperatorP ++ = 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 OperatorSample Operator ExpressionDescription/ Explanation
Prefix Increment Operator++pp increases by a value of 1, then the program uses the value of p.
Postfix Increment Operatorp++The program uses the current value of p and increases the value of p by 1.
Prefix Decrement Operator–pp decreases by a value of 1, then the program uses the value of p.
Postfix Decrement Operatorp–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 OperatorOperatorDescription of OperatorExample
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 OperatorOperatorExample
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:

aba & ba | ba ^ b
11110
00000
01011
10011

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 OperatorDescription of OperatorOperatorExample
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 OperatorOperatorExample
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 OperatorAssociativityCategory
() [] -> . ++ – –Left to rightPostfix
– + ! ~ – – ++ (type)* & sizeofRight to leftThe Unary Operator
/ * %Left to rightThe Multiplicative Operator
– +Left to rightThe Additive Operator
>> <<Left to rightThe Shift Operator
< > >= <=Left to rightThe Relational Operator
!= ==Left to rightThe Equality Operator
&Left to rightBitwise AND
^Left to rightBitwise XOR
|Left to rightBitwise OR
&&Left to rightLogical AND
||Left to rightLogical OR
?:Right to leftConditional
= -= += /= *= %=>>= &= <<= |= ^=Right to leftAssignment
,Left to rightComma

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,

  • Logical Operators in C
  • Operators in C
  • Relational Operators in C
  • Comma Operator in C
  • Modulus Operator in C
  • Constants in C
  • Data Types in C
  • Double Data Type in C
Operators in C | GATE Notes (2024)
Top Articles
Is it smart to share a credit card with your spouse?
Root certificate which is not trusted by the trust provider. (0x800b0109) - Microsoft Q&A
Katie Pavlich Bikini Photos
Gamevault Agent
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Free Atm For Emerald Card Near Me
Craigslist Mexico Cancun
Hendersonville (Tennessee) – Travel guide at Wikivoyage
Doby's Funeral Home Obituaries
Vardis Olive Garden (Georgioupolis, Kreta) ✈️ inkl. Flug buchen
Select Truck Greensboro
How To Cut Eelgrass Grounded
Pac Man Deviantart
Alexander Funeral Home Gallatin Obituaries
Craigslist In Flagstaff
Shasta County Most Wanted 2022
Energy Healing Conference Utah
Testberichte zu E-Bikes & Fahrrädern von PROPHETE.
Aaa Saugus Ma Appointment
Geometry Review Quiz 5 Answer Key
Walgreens Alma School And Dynamite
Bible Gateway passage: Revelation 3 - New Living Translation
Yisd Home Access Center
Home
Shadbase Get Out Of Jail
Gina Wilson Angle Addition Postulate
Celina Powell Lil Meech Video: A Controversial Encounter Shakes Social Media - Video Reddit Trend
Walmart Pharmacy Near Me Open
Dmv In Anoka
A Christmas Horse - Alison Senxation
Ou Football Brainiacs
Access a Shared Resource | Computing for Arts + Sciences
Pixel Combat Unblocked
Umn Biology
Cvs Sport Physicals
Mercedes W204 Belt Diagram
Rogold Extension
'Conan Exiles' 3.0 Guide: How To Unlock Spells And Sorcery
Teenbeautyfitness
Weekly Math Review Q4 3
Facebook Marketplace Marrero La
Nobodyhome.tv Reddit
Topos De Bolos Engraçados
Gregory (Five Nights at Freddy's)
Grand Valley State University Library Hours
Holzer Athena Portal
Hampton In And Suites Near Me
Stoughton Commuter Rail Schedule
Bedbathandbeyond Flemington Nj
Free Carnival-themed Google Slides & PowerPoint templates
Otter Bustr
Selly Medaline
Latest Posts
Article information

Author: Rev. Porsche Oberbrunner

Last Updated:

Views: 5567

Rating: 4.2 / 5 (73 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Rev. Porsche Oberbrunner

Birthday: 1994-06-25

Address: Suite 153 582 Lubowitz Walks, Port Alfredoborough, IN 72879-2838

Phone: +128413562823324

Job: IT Strategist

Hobby: Video gaming, Basketball, Web surfing, Book restoration, Jogging, Shooting, Fishing

Introduction: My name is Rev. Porsche Oberbrunner, I am a zany, graceful, talented, witty, determined, shiny, enchanting person who loves writing and wants to share my knowledge and understanding with you.