Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Operators in C programming are essential for performing various operations on data, including arithmetic, logical, and relational comparisons. Operators in C help manipulate variables and values efficiently, making them a fundamental concept in programming. This article explores different operators in C programming, their types, functionalities, and practical examples to enhance understanding and improve coding efficiency.
What are Operators in C?
Operators in C are the fundamental elements that drive logic and computation within programs. They are crucial for performing operations like arithmetic, comparison, logical operations, etc., and form the backbone of decision-making and problem-solving in programming.
For example
c = a + b;
Here, ‘+’ is the operator known as the addition operator, and ‘a’ and ‘b’ are operands. The addition operator tells the compiler to add both of the operands ‘a’ and ‘b’. To dive deeper into how operators are used with data structures,
Types of Operators in C :
Arithmetic Operators
Relational Operators
Bitwise Operators
Shift Operators
Ternary or Conditional Operators
Logical Operators
Assignment Operators
Misc Operators
Let's discuss them in detail:
1. Arithmetic Operators in C
Arithmetic operators perform basic mathematical operations: addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). These operators are used to calculate and manipulate numerical values in programming and mathematical expressions.
Operator
Description
Example
+
Addition
a + b
-
Subtraction
a - b
*
Multiplication
a * b
/
Division
a / b
%
Modulus (Remainder)
a % b
For example:
C
C
#include <stdio.h>
int main() { int a = 10, b = 5; printf("Addition: %d\n", a + b); printf("Subtraction: %d\n", a - b); printf("Multiplication: %d\n", a * b); printf("Division: %d\n", a / b); printf("Modulus: %d\n", a % b); return 0; }
Relational operators compare two values or expressions:
Equal to (==): Checks if two values are the same.
Not equal to (!=): Checks if two values are different.
Greater than (>): Checks if the left value is larger.
Less than (<): Checks if the left value is smaller.
Greater than or equal to (>=): Checks if the left value is larger or equal.
Less than or equal to (<=): Checks if the left value is smaller or equal.
These operators return a boolean value (true or false) and are essential for controlling the flow of programs using conditional statements like if, while, and for loops.
Operator
Description
Example
==
Equal to
a == b
!=
Not equal to
a != b
>
Greater than
a > b
<
Less than
a < b
>=
Greater than or equal to
a >= b
<=
Less than or equal to
a <= b
For example:
C
C
#include <stdio.h>
int main() { int a = 5, b = 10; printf("%d == %d is %d\n", a, b, a == b); printf("%d != %d is %d\n", a, b, a != b); printf("%d > %d is %d\n", a, b, a > b); printf("%d < %d is %d\n", a, b, a < b); printf("%d >= %d is %d\n", a, b, a >= b); printf("%d <= %d is %d\n", a, b, a <= b); return 0; }
Bitwise operators are used to perform operations at the bit level. Bitwise operators perform operations on the binary representations of integers. They include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). These operators manipulate individual bits within integer values, enabling low-level data processing, such as setting, clearing, and toggling specific bits, commonly used in system programming and performance-critical applications.
Operator
Description
Example
&
Bitwise AND
a & b
`
`
Bitwise OR
^
Bitwise XOR
a ^ b
~
Bitwise NOT
~a
<<
Left shift
a << b
>>
Right shift
a >> b
For example:
C
C
#include <stdio.h>
int main() { int a = 5, b = 3; printf("%d & %d is %d\n", a, b, a & b); printf("%d | %d is %d\n", a, b, a | b); printf("%d ^ %d is %d\n", a, b, a ^ b); printf("~%d is %d\n", a, ~a); printf("%d << %d is %d\n", a, b, a << b); printf("%d >> %d is %d\n", a, b, a >> b); return 0; }
The ternary operator (? :) provides a concise way to express conditional statements. It evaluates a condition and returns one of two values based on its truth.
For example:
C
C
#include <stdio.h>
int main() { int a = 5, b = 3; int max = (a > b) ? a : b;
The sizeof operator returns the size, in bytes, of a variable or data type. The comma operator allows multiple expressions in a single statement, evaluating them from left to right.
For example:
C
C
#include <stdio.h>
int main() { int a = 5; printf("The size of 'a' is %lu bytes\n", sizeof(a)); printf("The address of 'a' is %p\n", (void*)&a);
Logical Operators: Combine conditions using AND (&&), OR (||).
3. Ternary Operator: The only ternary operator in C is the conditional operator (?:). It takes three operands and evaluates a condition to decide which value to return.
Operator Precedence and Associativity in C
Precedence
Operators
Description
Associativity
1
(), [], ->, .
Parentheses, Array Subscript, Arrow Operator, Member Access
This table helps in understanding how expressions are evaluated in C programming. Operators with higher precedence are evaluated first, and in case of equal precedence, associativity determines the order of execution.
Frequently Asked Questions
What is the == operator in C?
The == operator in C is used for equality comparison between two operands.
What is the use of Operators in C programming?
Operators in C programming are used to perform various operations on variables and values, such as arithmetic, logical, relational, and bitwise operations. They facilitate calculations, comparisons, and data manipulation, enabling effective control and functionality within the program.
How many operators can C have?
C has a wide range of operators, including arithmetic, relational, logical, bitwise, assignment, increment/decrement, conditional (ternary), and special operators. There are over 45 operators, each serving specific operations and functionalities in programming.
What is the difference between the ‘=’ and ‘==’ operators?
The = operator is an assignment operator used to assign a value to a variable, while the == operator is a relational operator used to compare two values for equality, returning true if they are equal and false otherwise.
Conclusion
In this article, we talked about operators in C programming. We explored arithmetic, relational, logical, bitwise, and assignment operators. These operators allow developers to perform calculations, compare values, make decisions, manipulate bits, and assign values to variables. Operators are crucial for writing efficient and effective C code, that enables developers to solve complex problems easily.