Tuesday 30 July 2024

Operator in C

In C programming, operators are symbols that tell the compiler to perform specific mathematical or logical functions. Operators in C can be classified into several categories:



### 1. Arithmetic Operators

These operators perform arithmetic operations on variables and data.

 

```c

+   // Addition

-   // Subtraction

*   // Multiplication

/   // Division

%   // Modulus (remainder of division)

```

 

Example:

```c

int a = 10, b = 3;

int sum = a + b;       // 13

int diff = a - b;      // 7

int prod = a * b;      // 30

int quotient = a / b;  // 3

int remainder = a % b; // 1

```

 

### 2. Relational Operators

These operators compare two values and return true or false.

 

```c

==  // Equal to

!=  // Not equal to

>   // Greater than

<   // Less than

>=  // Greater than or equal to

<=  // Less than or equal to

```

 

Example:

```c

int a = 10, b = 3;

int result1 = (a == b);  // 0 (false)

int result2 = (a != b);  // 1 (true)

int result3 = (a > b);   // 1 (true)

int result4 = (a < b);   // 0 (false)

int result5 = (a >= b);  // 1 (true)

int result6 = (a <= b);  // 0 (false)

```

 

### 3. Logical Operators

These operators are used to combine conditional statements.

 

```c

&&  // Logical AND

||  // Logical OR

!   // Logical NOT

```

 

Example:

```c

int a = 10, b = 3, c = 0;

int result1 = (a > b && b > c);  // 1 (true)

int result2 = (a > b || b > c);  // 1 (true)

int result3 = !(a > b);          // 0 (false)

```

 

### 4. Bitwise Operators

These operators perform bit-level operations on data.

 

```c

&   // Bitwise AND

|   // Bitwise OR

^   // Bitwise XOR

~   // Bitwise NOT

<<  // Left shift

>>  // Right shift

```

 

Example:

```c

int a = 5;      // 0101 in binary

int b = 3;      // 0011 in binary

 

int result1 = a & b;  // 0001 (1)

int result2 = a | b;  // 0111 (7)

int result3 = a ^ b;  // 0110 (6)

int result4 = ~a;     // 1010 (binary for -6 in 2's complement)

int result5 = a << 1; // 1010 (10)

int result6 = a >> 1; // 0010 (2)

```

 

### 5. Assignment Operators

These operators assign values to variables.

 

```c

=   // Assign

+=  // Add and assign

-=  // Subtract and assign

*=  // Multiply and assign

/=  // Divide and assign

%=  // Modulus and assign

```

 

Example:

```c

int a = 10;

a += 5;  // a = a + 5, now a is 15

a -= 3;  // a = a - 3, now a is 12

a *= 2;  // a = a * 2, now a is 24

a /= 4;  // a = a / 4, now a is 6

a %= 5;  // a = a % 5, now a is 1

```

 

### 6. Increment and Decrement Operators

These operators increase or decrease the value of a variable by one.

 

```c

++  // Increment

--  // Decrement

```

 

Example:

```c

int a = 10;

a++;  // a is now 11

a--;  // a is now 10 again

 

int b = 5;

int c = ++b;  // c is 6, b is 6 (pre-increment)

int d = b--;  // d is 6, b is 5 (post-decrement)

```

 

### 7. Conditional (Ternary) Operator

This operator evaluates a condition and returns one of two values.

 

```c

condition ? expression1 : expression2

```

 

Example:

```c

int a = 10, b = 5;

int max = (a > b) ? a : b;  // max is 10

```

 

### 8. Special Operators

- **Comma Operator**: Used to separate two or more expressions.

- **Sizeof Operator**: Returns the size of a data type or variable.

- **Pointer Operators**: `&` (address of), `*` (value at address).

 

Example:

```c

int a = 10, b = 5;

int result = (a++, b++);  // result is 5, a is 11, b is 6

 

int size = sizeof(a);  // size is typically 4 (depending on the system)

 

int *p = &a;  // p is a pointer to a

int value = *p;  // value is 11

```

 

These are some of the fundamental operators in C, which are essential for performing various operations and building complex expressions in your programs.

TCCI Computer classes provide the best training in all computer courses online and offline through different learning methods/media located in Bopal Ahmedabad and ISCON Ambli Road in Ahmedabad.

For More Information:      

Call us @ +91 98256 18292

Visit us @ http://tccicomputercoaching.com/

No comments:

Post a Comment