C operators
Created By: chatGPT
C operators are symbols that tell the compiler to perform specific mathematical or logical manipulations. They can be classified into several categories, including arithmetic, relational, logical, bitwise, assignment, and more. Understanding these operators is crucial for writing efficient and effective C programs.Here is an overview of some common arithmetic operators:Addition (+), Subtraction (-), Multiplication (*), Division (/), and Modulus (%) are the primary arithmetic operators used to perform basic math operations.
Example usage of arithmetic operators:
Relational operators allow you to compare two values. They return boolean results (true or false). The primary relational operators include:Equal to (==), Not equal to (!=), Greater than (>), Less than (<), Greater than or equal to (>=), Less than or equal to (<=).int a = 10;
int b = 20;
int sum = a + b;
int product = a * b;
int division = b / a;
int remainder = b % a;
Example for relational operators:
Logical operators are used to perform logical operations, commonly used in conditional statements. They include:Logical AND (&&), Logical OR (||), Logical NOT (!).if (a == b) {
// Code to execute if a is equal to b
}
if (a < b) {
// Code to execute if a is less than b
}
Example for logical operators:
Bitwise operators are used to perform operations on bits. They include:Bitwise AND (&), Bitwise OR (|), Bitwise XOR (^), Bitwise NOT (~), Left Shift (<<), and Right Shift (>>).if (a < b && b > 0) {
// Code if both conditions are true
}
if (!(a == b)) {
// Code if a is not equal to b
}
Example for bitwise operators:
Assignment operators are used to assign values to variables. The most common include:Simple assignment (=), Add and assign (+=), Subtract and assign (-=), Multiply and assign (*=), Divide and assign (/=), Modulus and assign (%=).int x = 5; // 0101 in binary
int y = 3; // 0011 in binary
int result = x & y; // Result is 1 (0001 in binary)
Example for assignment operators:
In summary, C operators are essential building blocks for performing operations in your programs. Mastering them will help you become proficient in C programming.int z = 10;
z += 5; // z is now 15
z *= 2; // z is now 30