C Programs Tutorials | IT Developer
IT Developer

C Programming - C Operators



Share with a Friend

C Programming - C Operators

C Operators

Operators in C are special symbols or keywords used to perform specific operations on variables or values. C supports a rich set of operators for various operations such as arithmetic, logical, relational, bitwise, and more.

Types of Operators

  1. Arithmetic Operators

Arithmetic operators are used to perform mathematical calculations.

Operator Description Example

+

Addition

a + b

-

Subtraction

a - b

*

Multiplication

a * b

/

Division

a / b

%

Modulus (remainder)

a % b

  1. Relational (Comparison) Operators

Relational operators are used to compare two values.

Operator Description Example

==

Equal to

a == b

!=

Not equal to

a != b

Less than

a < b

Greater than

a > b

<=

Less than or equal to

a <= b

>=

Greater than or equal to

a >= b

  1. Logical Operators

Logical operators are used to perform logical operations.

Operator Description Example

&&

Logical AND

(a > b) && (c > d)

`

 

`

!

Logical NOT

!(a > b)

  1. Bitwise Operators

Bitwise operators work on binary representations of integers.

Operator Description Example

Operator

Description

Example

&

Bitwise AND

a & b

`

`

Bitwise OR

^

Bitwise XOR

a ^ b

~

Bitwise NOT

~a

<< 

Left shift

a << 2

>> 

Right shift

a >> 2

  1. Assignment Operators

Assignment operators are used to assign values to variables.

Operator Description Example

=

Assign value

a = 5

+=

Add and assign

a += 5

-=

Subtract and assign

a -= 5

*=

Multiply and assign

a *= 5

/=

Divide and assign

a /= 5

%=

Modulus and assign

a %= 5

  1. Increment and Decrement Operators

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

Operator Description Example

++

Increment

++a, a++

--

Decrement

--a, a--

  1. Conditional (Ternary) Operator

The ternary operator is a shorthand for if-else conditions.

Operator Description Example

? :

Ternary conditional

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

  1. Special Operators
  • Sizeof Operator: Used to find the size of a data type or variable.

C

int a = 5;

printf("%lu", sizeof(a)); // Outputs size of 'a' in bytes

  • Comma Operator: Used to separate expressions.

C

int a, b = (1, 2); // b gets the value 2

  • Pointer Operators:
Operator Description Example

*

Dereference operator

*ptr

&

Address-of operator

&var

Operator Precedence and Associativity

Precedence: Determines the order of evaluation of operators.

Associativity: Determines the direction of evaluation (left-to-right or right-to-left).

Precedence Operators Associativity

Highest

(), [], ->, .

Left-to-right

 

++, -- (Postfix)

Left-to-right

 

+, -, *, /, %

Left-to-right

Lowest

=, +=, -= (Assignment)

Right-to-left

Examples

Arithmetic Example

C

int a = 10, b = 20;

printf("Sum: %d", a + b);  // Outputs: Sum: 30

Relational Example

C

if (a > b) {

    printf("a is greater");

} else {

    printf("b is greater");

}

Logical Example

C

if ((a > b) && (b > 0)) {

    printf("Condition satisfied");

}

Bitwise Example

C

int a = 5;  // Binary: 0101

int b = 3;  // Binary: 0011

printf("Bitwise AND: %d", a & b); // Outputs: 1 (0001)

Key Points

  1. Use parentheses to ensure clarity and desired evaluation order.
  2. Be cautious with operator precedence to avoid unintended results.
  3. Understand the data type compatibility for bitwise and arithmetic operations.
  4. Use logical operators for condition checks and bitwise operators for low-level manipulations.