C Operators

Operators in C programming are symbols that represent operations or calculations. They allow you to perform various tasks, such as arithmetic operations, comparisons, and logical manipulations.

Arithmetic Operators

Arithmetic operators are used for performing mathematical calculations.

Examples:

+ (addition), - (subtraction), * (multiplication), / (division), % (modulo, remainder).

int a = 10, b = 4; int sum = a + b; // sum is 14 int difference = a - b; // difference is 6 int product = a * b; // product is 40 int quotient = a / b; // quotient is 2 int remainder = a % b; // remainder is 2

Assignment Operators

Assignment operators are used to assign values to variables.

Examples:

= (assignment), += (addition assignment), -= (subtraction assignment), *= (multiplication assignment), /= (division assignment), %= (modulo assignment).

int x = 5; x += 3; // equivalent to x = x + 3; (x is now 8) x -= 2; // equivalent to x = x - 2; (x is now 6)

Comparison Operators

Comparison operators are used to compare two values and produce a Boolean result (true or false).

Examples:

== (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to).

int p = 7, q = 3; bool isEqual = (p == q); // isEqual is false bool isNotEqual = (p != q); // isNotEqual is true bool isLess = (p < q); // isLess is false

Logical Operators

Logical operators are used to perform logical operations on Boolean values.

Examples: && (logical AND), || (logical OR), ! (logical NOT).
bool isTrue = true, isFalse = false; bool result1 = isTrue && isFalse; // result1 is false bool result2 = isTrue isFalse; // result2 is true bool result3 = !isTrue; // result3 is false

Increment and Decrement Operators

Increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1. They can be used as prefix (++i) or postfix (i++).

int counter = 5; counter++; // Increment counter by 1 (counter is now 6) counter--; // Decrement counter by 1 (counter is now 5 again)

Bitwise Operators

Bitwise operators perform operations on individual bits of integers.

Examples: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (left shift), >> (right shift).
int num1 = 5, num2 = 3; int result1 = num1 & num2; // Bitwise AND (result1 is 1) int result2 = num1 num2; // Bitwise OR (result2 is 7)

These operators are essential tools for performing various calculations and comparisons in C programming, enabling you to manipulate data and control program flow effectively.

Conclusion

Operators in C programming are symbols that perform various operations on data. They include arithmetic operators for mathematical calculations, assignment operators for variable assignment, comparison operators for evaluating conditions, logical operators for Boolean logic, and more. These operators are fundamental to performing a wide range of tasks and controlling program behavior in C.