Chapter 6: Operators in C Language

Chapter 6: Operators in C Language

In C language, operators are used to perform various operations. C language includes a rich set of operators that can process data, perform comparisons, and execute logical operations. The types of operators are numerous and are generally divided into the following categories:

  1. 1. Arithmetic Operators
  2. 2. Relational Operators
  3. 3. Logical Operators
  4. 4. Bitwise Operators (Understand)
  5. 5. Assignment Operators
  6. 6. Increment and Decrement Operators
  7. 7. Conditional Operator (Ternary Operator)
  8. 8. Bit Field Operators (Understand)
  9. 9. Pointer Operators (Understand)

Below, we will introduce each category of operators in detail.

1. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations, including addition, subtraction, multiplication, division, and modulus.

Operator Description Example
<span>+</span> Addition <span>a + b</span>
<span>-</span> Subtraction <span>a - b</span>
<span>*</span> Multiplication <span>a * b</span>
<span>/</span> Division <span>a / b</span>
<span>%</span> Modulus <span>a % b</span>

Example:

int a = 10, b = 3;
int sum = a + b;  // Result is 13
int difference = a - b;  // Result is 7
int product = a * b;  // Result is 30
int quotient = a / b;  // Result is 3
int remainder = a % b;  // Result is 1

2. Relational Operators

Relational operators are used to compare the relationship between two operands, returning a value of <span>1</span> (true) or <span>0</span> (false). They are commonly used to determine size relationships or equality.

Operator Description Example
<span>==</span> Equal to <span>a == b</span>
<span>!=</span> Not equal to <span>a != b</span>
<span>></span> Greater than <span>a > b</span>
<span><</span> Less than <span>a < b</span>
<span>>=</span> Greater than or equal to <span>a >= b</span>
<span><=</span> Less than or equal to <span>a <= b</span>

Example:

int a = 5, b = 3;
int result1 = a == b;  // Result is 0 (false)
int result2 = a > b;   // Result is 1 (true)

3. Logical Operators

Logical operators are used to connect multiple conditional expressions, commonly used in control flow conditions.

Operator Description Example
<span>&&</span> Logical AND <span>a > 5 && b < 10</span>
` `
<span>!</span> Logical NOT <span>!(a > 5)</span>

Example:

int a = 5, b = 3;
int result1 = (a > 5) && (b < 10);  // Result is 0 (false)
int result2 = (a > 5) || (b < 10);  // Result is 1 (true)
int result3 = !(a > 5);             // Result is 1 (true)

4. Bitwise Operators

Bitwise operators are used to perform bit-level operations on integer type operands. They are suitable for processing binary data.

Operator Description Example
<span>&</span> Bitwise AND <span>a & b</span>
` ` Bitwise OR
<span>^</span> Bitwise XOR <span>a ^ b</span>
<span>~</span> Bitwise NOT <span>~a</span>
<span><<</span> Left Shift <span>a << 2</span>
<span>>></span> Right Shift <span>a >> 2</span>

Example:

int a = 5, b = 3;  // a = 0101, b = 0011
int result1 = a && b;  // Bitwise AND Result is 1 (0001)
int result2 = a | b;  // Bitwise OR Result is 7 (0111)
int result3 = a ^ b;  // Bitwise XOR Result is 6 (0110)
int result4 = ~a;     // Bitwise NOT Result is -6 (11111111111111111111111111111010)
int result5 = a << 2; // Left Shift Result is 20 (10100)
int result6 = a >> 2; // Right Shift Result is 1 (0001)

5. Assignment Operators

Assignment operators are used to assign the value on the right to the variable on the left.

Operator Description Example
<span>=</span> Assignment <span>a = b</span>
<span>+=</span> Addition Assignment <span>a += b</span>
<span>-=</span> Subtraction Assignment <span>a -= b</span>
<span>*=</span> Multiplication Assignment <span>a *= b</span>
<span>/=</span> Division Assignment <span>a /= b</span>
<span>%=</span> Modulus Assignment <span>a %= b</span>

Example:

int a = 5, b = 3;
a += b;  // a = a + b, Result is 8
a -= b;  // a = a - b, Result is 5
a *= b;  // a = a * b, Result is 15
a /= b;  // a = a / b, Result is 5
a %= b;  // a = a % b, Result is 2

6. Increment and Decrement Operators

Increment (<span>++</span>) and Decrement (<span>--</span>) operators are used to increase or decrease the value of a variable by 1. They can be used as either prefix or postfix.

Operator Description Example
<span>++</span> Increment <span>a++</span> or <span>++a</span>
<span>--</span> Decrement <span>a--</span> or <span>--a</span>

Example:

int a = 5;
int result1 = a++;  // Postfix increment, result1 = 5, a becomes 6
int result2 = ++a;  // Prefix increment, a becomes 7, result2 = 7

7. Conditional Operator (Ternary Operator)

The conditional operator (ternary operator) is used to select a value based on the truth of a condition. Its syntax is as follows:

condition ? expression1 : expression2;

If the condition is true, it returns <span>expression1</span>, otherwise it returns <span>expression2</span>.

Example:

int a = 5, b = 3;
int result = (a > b) ? a : b;  // Result is 5, because a > b

8. Bit Field Operators

Bit field operators are used to manipulate bit fields (bit fields in structures), allowing precise control over bits in memory.

struct BitField {
    unsigned int a: 3;
    unsigned int b: 5;
};

9. Pointer Operators

Pointer operators are used to manipulate pointers. Common pointer operators include <span>&</span> and <span>*</span>.

Operator Description Example
<span>&</span> Address-of Operator <span>&a</span>
<span>*</span> Dereference Operator <span>*ptr</span>

Example:

int a = 5;
int *ptr = &a;  // & gets the address
int value = *ptr;  // * dereferences, getting the value pointed to by ptr, result is 5

10. Summary

Arithmetic OperatorsRelational OperatorsLogical OperatorsAssignment OperatorsIncrement and Decrement Operators

Leave a Comment