Operators in C programming are symbols or special keywords used to perform various operations on data or variables. They are a fundamental part of any programming language and are essential for performing tasks like arithmetic calculations, comparisons, logical operations, and more. Here, I'll provide an overview of the most common types of operators in C:
Arithmetic Operators:
+
(Addition): Adds two values.-
(Subtraction): Subtracts the right operand from the left operand.*
(Multiplication): Multiplies two values./
(Division): Divides the left operand by the right operand.%
(Modulus): Returns the remainder after division.
Example:
cint a = 10, b = 3, result; result = a + b; // result contains 13
Relational Operators:
==
(Equal to): Checks if two values are equal.!=
(Not equal to): Checks if two values are not equal.>
(Greater than): Checks if the left operand is greater than the right operand.<
(Less than): Checks if the left operand is less than the right operand.>=
(Greater than or equal to): Checks if the left operand is greater than or equal to the right operand.<=
(Less than or equal to): Checks if the left operand is less than or equal to the right operand.
Example:
cint x = 5, y = 10; if (x < y) { // This condition is true }
Logical Operators:
&&
(Logical AND): Returns true if both conditions are true.||
(Logical OR): Returns true if at least one condition is true.!
(Logical NOT): Negates a condition.
Example:
cint p = 1, q = 0; if (p && q) { // This condition is false }
Assignment Operators:
=
(Assignment): Assigns a value to a variable.+=
,-=
(Addition Assignment, Subtraction Assignment): Adds or subtracts the right operand from the left operand and assigns the result.*=
,/=
,%=
(Multiplication Assignment, Division Assignment, Modulus Assignment): Performs the respective operation and assigns the result.
Example:
cint num = 10; num += 5; // num contains 15
Increment and Decrement Operators:
++
(Increment): Increases the value of a variable by 1.--
(Decrement): Decreases the value of a variable by 1.
Example:
cint count = 5; count++; // count contains 6
Bitwise Operators:
&
(Bitwise AND): Performs bitwise AND operation.|
(Bitwise OR): Performs bitwise OR operation.^
(Bitwise XOR): Performs bitwise XOR operation.<<
(Left Shift): Shifts bits to the left.>>
(Right Shift): Shifts bits to the right.
Example:
cint a = 5, b = 3; int result = a & b; // result contains 1 (binary 0101 & 0011 = 0001)
These are some of the fundamental operators in C programming. They allow you to perform a wide range of operations, making C a versatile language for various applications.
Follow us