Operators in C Language: Types and Examples
Table of Contents
In the realm of programming, operators play a key role in performing operations on variables and values. And, when it comes to the C language – one of the most popular and widely used programming languages—understanding the role of these operators is crucial to writing efficient and effective code.
Operators in C are special symbols or keywords that instruct the compiler to perform specific operations. They are used for versatile purposes like arithmetic calculations, determining relations, decision-making, calculating memory size, and more.
The following write-up will explore different types of operators in the C language, their functionalities, and syntaxes for a detailed understanding.
*logicmojo.com
Types of Operators in C
Operators in C help to perform different types of operations between the variables or values. They are used in the expressions to conduct their tasks. So, the expressions are the combinations of variables, operators, and constants. The data items on which the operators act are called the operands.
Here are the different types of operators used in C –
Arithmetic operators in C
The basic mathematical operations in the C programming language are performed by arithmetic operators. These include basic addition, subtraction, division, multiplication, modulus, etc. These operators in C work on numeric data types. That means they work on integer and float data types. They help to perform both the simple and complex operations efficiently.
Symbol | Operator | Description | Syntax |
---|---|---|---|
+ | Plus | Simple addition of two numeric values. | a + b |
– | Minus | Simple subtraction from left operand to right operand. | a – b |
* | Multiply | Multiply two numeric values. | a * b |
/ | Divide | Divide two numeric values. | a / b |
% | Modulus | Returns the remainder after dividing the left operand by the right operand. | a % b |
+ | Unary Plus | Used to specify the positive values. | +a |
– | Unary Minus | Flips the sign of the value. | -a |
++ | Increment | Increases the value of the operand by 1. | a++ |
-- | Decrement | Decreases the value of the operand by 1. | a-- |
Example of arithmetic operators in C
#include
int main()
{
int a = 25,b = 10, c;
c = a+b;
printf(“a+b = %d n”,c);
c = a-b;
printf(“a-b = %d n”,c);
c = a*b;
printf(“a*b = %d n”,c);
c = a/b;
printf(“a/b = %d n”,c);
c = a%b;
printf(“Remainder when a is divided by b = %d n”,c);
return 0;
}
Output:
a+b = 35
a-b = 15
a*b = 250
a/b = 2
The remainder when a divided by b = 5
Logical operators in C
Logical operators in C combine conditions for returning true or false. For true, the output will be 1 and 0 for false. These operators help in deciding mankind in the conditional statements like if, for, and while.
Symbol | Operator | Description | Syntax |
---|---|---|---|
&& | Logical AND | Returns true if both the operands are true. | a && b |
|| | Logical OR | Returns true if both or any of the operand is true. | a || b |
! | Logical NOT | Returns true if the operand is false. | !a |
Example of logical operators in C
// Working of logical operators
#include
int main()
{
int a = 36, b = 36, c = 42, results;
results = (a == b) && (c > b);
printf(“(a == b) && (c > b) is %d n”, results);
results = (a == b) && (c < b);
printf(“(a == b) && (c < b) is %d n”, results);
results = (a == b) || (c < b);
printf(“(a == b) || (c < b) is %d n”, results);
results = (a != b) || (c < b);
printf(“(a != b) || (c < b) is %d n”, results); results = !(a != b); printf(“!(a != b) is %d n”, results); results = !(a == b); printf(“!(a == b) is %d n”, results); return 0;
}
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Relational operators in C
These operators in C determine the relations between the values of two operands during the operations. For example, they can determine whether one operand is greater than another, smaller than or equal to another.
Symbol | Operator | Description | Syntax |
---|---|---|---|
< | Less than | Returns true if the left operand is less than the right operand. Else false | a < b |
> | Greater than | Returns true if the left operand is greater than the right operand. Else false | a > b |
<= | Less than or equal to | Returns true if the left operand is less than or equal to the right operand. Else false | a <= b |
>= | Greater than or equal to | Returns true if the left operand is greater than or equal to the right operand. Else false | a >= b |
== | Equal to | Returns true if both operands are equal. | a == b |
!= | Not equal to | Returns true if both operands are NOT equal. | a != b |
Example of relational operators in C
#include <stdio.h>
int main() {
int a = 51, b = 7;
// using operators and printing results
printf(“a < b : %d\n”, a < b);
printf(“a > b : %d\n”, a > b);
printf(“a <= b: %d\n”, a <= b);
printf(“a >= b: %d\n”, a >= b);
printf(“a == b: %d\n”, a == b);
printf(“a != b : %d\n”, a != b);
return 0;
}
Output
a < b : 0
a > b : 1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Bitwise operators in C
Bitwise operators in C enable operations for bit by bit of the integer values. So, these operators are used in bitwise operations for the integers that are significant in low-level programming, hardware interactions, and cryptography. These operations are performed as a binary aspect.
Symbol | Operator | Description | Syntax |
---|---|---|---|
& | Bitwise AND | Performs a bit-by-bit AND operation and returns the result. | a & b |
| | Bitwise OR | Performs a bit-by-bit OR operation and returns the result. | a | b |
^ | Bitwise XOR | Performs a bit-by-bit XOR operation and returns the result. | a ^ b |
~ | Bitwise First Complement | Flips all the set and unset bits on the number. | ~a |
<< | Bitwise Left Shift | Shifts the number in binary form by one place left in the operation and returns the result. | a << b |
>> | Bitwise Right Shift | Shifts the number in binary form by one place right in the operation and returns the result. | a >> b |
Example of bitwise operators in C
#include <stdio.h>
int main() {
int a = 45, b = 9;
// using operators and printing results
printf(“a & b: %d\n”, a & b);
printf(“a | b: %d\n”, a | b);
printf(“a ^ b: %d\n”, a ^ b);
printf(“~a: %d\n”, ~a);
printf(“a >> b: %d\n”, a >> b);
printf(“a << b: %d\n”, a << b);
return 0;
}
Output
a & b: 9
a | b: 45
a ^ b: 36
~a: -46
a >> b: 5
a << b: 360
Assignment operators in C
To assign the values to the variables, the assignment operators in C are used. ‘=’ is the basic assignment operator that assigns a value (right hand) to a variable (left hand). Additionally, C also includes more compound assignment operators like +=, -=, etc.
Symbol | Operator | Description | Syntax |
---|---|---|---|
= | Simple Assignment | Assign the value of the right operand to the left operand. | a = b |
+= | Plus and assign | Add the right operand and left operand and assign this value to the left operand. | a += b |
-= | Minus and assign | Subtract the right operand and left operand and assign this value to the left operand. | a -= b |
*= | Multiply and assign | Multiply the right operand and left operand and assign this value to the left operand. | a *= b |
/= | Divide and assign | Divide the left operand with the right operand and assign this value to the left operand. | a /= b |
%= | Modulus and assign | Assign the remainder in the division of left operand with the right operand to the left operand. | a %= b |
&= | AND and assign | Performs bitwise AND and assigns this value to the left operand. | a &= b |
|= | OR and assign | Performs bitwise OR and assigns this value to the left operand. | a |= b |
^= | XOR and assign | Performs bitwise XOR and assigns this value to the left operand. | a ^= b |
>>= | Right Shift and assign | Performs a bitwise right shift and assigns this value to the left operand. | a >>= b |
<<= | Left Shift and assign | Performs a bitwise left shift and assigns this value to the left operand. | a <<= b |
Example of assignment operators in C
// C program to demonstrate assignment operators
#include <stdio.h>
int main() {
int a = 11, b = 6;
// Basic assignment
a = b;
printf(“a = %d\n”, a); // a becomes 5
// Compound assignment operators
a += 3; // equivalent to a = a + 3
printf(“a += 3 -> %d\n”, a);
a *= 2; // equivalent to a = a * 2
printf(“a *= 2 -> %d\n”, a);
a -= 4; // equivalent to a = a – 4
printf(“a -= 4 -> %d\n”, a);
a /= 3; // equivalent to a = a / 3
printf(“a /= 3 -> %d\n”, a);
return 0;
}
Output
a = 6
a += 3 -> 9
a *= 2 -> 18
a -= 4 -> 14
a /= 3 -> 4
Other Types of Operators in C
Apart from the above mentioned operators, there are some special operators in C. Here is the list of those operators –
Symbol | Operator | Description | Syntax |
---|---|---|---|
? : | Ternary or conditional | A type of if-else statement, but a shorthand | condition? a : b |
, | Comma | Helps to separate expressions in a list, evaluated from left to right. | a, b |
. | Member access (Direct) | To access the members of a union or structure directly | structure.member |
() | Function call | Executes a function | function() |
[] | Array subscript | To access the elements of an array | array[index] |
sizeof | Sizeof | To return the size of the variables or data types in bytes | sizeof(a) or sizeof(int) |
-> | Member access (Indirect) | To access the members of a union or structure through a pointer | pointer->member |
* | Pointer dereference | To access the value pointed to by a pointer | *ptr |
(type) | Type cast | To convert a value from one data type to another, explicitly | (int)float_var |
& | Address-of | To return the memory address of a variable | &variable |
Significance of Operators in C
Here is why the operators in C are necessary –
- Perform mathematical calculations: The arithmetic operators in C help to perform the basic mathematical calculations such as addition, multiplication, subtraction, and division.
- Maintain the program’s flow: The logical operators in C help in the decision-making process of conditional statements. This way, they maintain the program’s flow and reduce code redundancy.
- Improve readability: The ternary operators make the conditional logic readable and concise.
- Handle pointers and memory: Special operators like * and & are mandatory to efficiently handle operations regarding pointers, manipulation, and memory allocation.
- Simplify code: The operators like arithmetic and assignment simplify the code by reducing the necessity of writing longer expressions.
*logicmojo.com
Unlocking Tech Potential with a Comprehensive BCA Program
C programming is a fundamental topic of computer science. It also provides the core concepts that help individuals to learn other programming languages. So, individuals interested in the C programming language should take a profession certification course that helps in their career growth. For example, they can take the Online BCA Programme – Manipal University Jaipur.
The course not only covers C programming but also covers advanced technologies like cloud technology, Big Data, machine learning, DBMS, etc. Apart from these, the course also guides the students to gain knowledge about financial accounting and management, personality development, and building impactful communication skills. These ways, the program supports the students to get ready for the dynamic world of the corporate sector.
Conclusion
So, this guide covered all the major operators in C. The operators are the core concepts of any programming language, and without them, the code can’t stand at all. Thus, students need extensive knowledge about the operators to efficiently write code in any programming language and improve their career prospects.
Frequently Asked Questions
What are the operators in C?
Operators are the symbols that instruct computers to perform specific operations. They are the pillars of a code that helps to manipulate data and build logic.
What are the logical operators in C?
The logical operators are those that work with the Boolean conditions.
What is the difference between ++a and a++ in C?
++a is the pre-increment, which indicates increments before use. However, a++ is the post-increment, which means increments after use.