Operators in C Language
Introduction to Operators in C Language
Operators in the C programming language are symbols that are used to perform operations on operands. These operations can include arithmetic, logical, relational, bitwise, and more. Understanding and utilizing operators are fundamental aspects of programming in C.
Operators serve as fundamental components in all programming languages, enabling a variety of actions on variables and values through symbolic representations. They facilitate arithmetic calculations, relational comparisons, and logical decision-making processes.
This chapter delves into the essential operators in C programming. It begins by exploring arithmetic operators, which enable basic mathematical computations. Subsequently, relational operators, also known as comparison operators, are introduced to facilitate value comparisons. Additionally, logical operators are discussed, offering the capability to formulate decisions based on conditions.
Beyond these foundational operators, the chapter covers supplementary operators like assignment operators, as well as increment and decrement operators. By the chapter’s conclusion, readers will possess a comprehensive understanding of leveraging diverse operators to manipulate data effectively.
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.
Addition operator (+)
The addition operator is used to add two operands together. For example, a + b
adds the values of a
and b
.
Subtraction operator (-)
The subtraction operator subtracts the value of the second operand from the first operand. For example, a - b
subtracts the value of b
from a
.
Multiplication operator (*)
The multiplication operator multiplies two operands. For example, a * b
multiplies the values of a
and b
.
Division operator (/)
The division operator divides the first operand by the second operand. For example, a / b
divides the value of a
by b
.
Modulus operator (%)
The modulus operator returns the remainder of the division operation. For example, a % b
returns the remainder when a
is divided by b
.
Relational Operators
Relational operators are used to compare two values and determine the relationship between them.
Equality operator (==)
The equality operator checks if two operands are equal. For example, a == b
evaluates to true if a
is equal to b
.
Inequality operator (!=)
The inequality operator checks if two operands are not equal. For example, a != b
evaluates to true if a
is not equal to b
.
Greater than operator (>)
The greater than operator checks if the value of the left operand is greater than the value of the right operand.
Less than operator (<)
The less than operator checks if the value of the left operand is less than the value of the right operand.
Greater than or equal to operator (>=)
The greater than or equal to operator checks if the value of the left operand is greater than or equal to the value of the right operand.
Less than or equal to operator (<=)
The less than or equal to operator checks if the value of the left operand is less than or equal to the value of the right operand.
Logical Operators
Logical operators are used to perform logical operations on two or more conditions.
AND operator (&&)
The AND operator returns true if both operands are true.
OR operator (||)
The OR operator returns true if at least one of the operands is true.
NOT operator (!)
The NOT operator returns the opposite boolean value of the operand.
Assignment Operators
Assignment operators are used to assign values to variables.
Simple assignment operator (=)
The simple assignment operator assigns the value of the right operand to the left operand.
*Compound assignment operators (+=, -=, =, /=, %=)
Compound assignment operators combine an arithmetic operation with the simple assignment operator.
Increment and Decrement Operators
Increment and decrement operators are used to increase or decrease the value of a variable by 1.
Increment operator (++)
The increment operator increases the value of a variable by 1.
Decrement operator (–)
The decrement operator decreases the value of a variable by 1.
Bitwise Operators
Bitwise operators perform operations on individual bits of operands.
AND operator (&)
The bitwise AND operator performs a bitwise AND operation on the operands.
OR operator (|)
The bitwise OR operator performs a bitwise OR operation on the operands.
XOR operator (^)
The bitwise XOR operator performs a bitwise XOR operation on the operands.
Complement operator (~)
The bitwise complement operator performs a bitwise NOT operation on the operand.
Left shift operator (<<)
The left shift operator shifts the bits of the left operand to the left by a specified number of positions.
Right shift operator (>>)
The right shift operator shifts the bits of the left operand to the right by a specified number of positions.
Conditional Operator (Ternary Operator)
The conditional operator is a ternary operator that evaluates a condition and returns one of two values depending on whether the condition is true or false.
Operator Precedence
Operator precedence determines the order in which operators are evaluated in an expression.
Examples and Practice
Let’s illustrate the usage of various operators with some code examples:
int main()
{
int a = 10, b = 5, c;
// Arithmetic Operators
c = a + b; // Addition
printf("a + b = %d\n", c);
c = a - b; // Subtraction
printf("a - b = %d\n", c);
c = a * b; // Multiplication
printf("a * b = %d\n", c);
c = a / b;
0 Comments