Nano Technology

Operators in C Programming


1. What are Operators in C?

In C programming, operators are special symbols used to perform operations on variables and values. They allow programmers to manipulate data, perform calculations, compare values, and control program flow.

An operator works on one or more operands and produces a result. Understanding operators is essential for writing correct and efficient C programs.

C Operators

2. Arithmetic Operators

Arithmetic operators are used to perform basic mathematical operations.

Operator Operation Example
+ Addition a + b
- Subtraction a - b
* Multiplication a * b
/ Division a / b
% Modulus a % b

3. Relational Operators

Relational operators are used to compare two values. The result of a relational operation is either 1 (true) or 0 (false).

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

4. Logical Operators

Logical operators are used to combine or negate relational expressions. They are commonly used in decision-making statements.

Operator Name Description
&& Logical AND True if both conditions are true
|| Logical OR True if at least one condition is true
! Logical NOT Reverses the condition

5. Assignment Operators

Assignment operators are used to assign values to variables. They can also perform operations while assigning values.

Operator Example Equivalent Expression
= a = 10 Assigns 10 to a
+= a += 5 a = a + 5
-= a -= 5 a = a - 5
*= a *= 2 a = a * 2
/= a /= 2 a = a / 2

6. Increment and Decrement Operators

Increment and decrement operators are used to increase or decrease the value of a variable by one.

Operator Type Example
++ Increment a++, ++a
-- Decrement a--, --a

Prefix operators modify the value before use, while postfix operators modify the value after use.

7. Bitwise Operators

Bitwise operators perform operations at the bit level. They are mainly used in system programming and embedded systems.

Operator Operation
& Bitwise AND
| Bitwise OR
^ Bitwise XOR
<< Left shift
>> Right shift

8. Conditional Operator

The conditional operator (?:) is a shorthand version of the if–else statement.

result = (a > b) ? a : b;
                        

If the condition is true, the first value is returned; otherwise, the second value is returned.

9. Operator Precedence

Operator precedence determines the order in which operators are evaluated in an expression. Operators with higher precedence are evaluated before operators with lower precedence.

For example:

int x = 5 + 2 * 3;   // Result = 11
                        

Here, multiplication has higher precedence than addition.

10. Examples


#include <stdio.h>

int main()
{
    int a = 10, b = 5;

    printf("Addition: %d\n", a + b);
    printf("Greater: %d\n", a > b);
    printf("Logical AND: %d\n", (a > 5 && b < 10));
    printf("Increment: %d\n", ++a);

    return 0;
}

This program demonstrates the use of arithmetic, relational, logical, and increment operators in C.

Note: A strong understanding of operators is essential before learning control statements, loops, arrays, and functions.