operators in c programming

v Operators: -

  • An operator is a symbol that tells or intimates to the compiler to perform specific calculations or manipulations on one or more operands.
  • Operators are essential for performing computations and making decisions in programs.

 

a + b is an operation

a & b are operands

+ is an operator

 

Based on number of operands involved:

1.     Unary operators: - unary operators can be applied on only one operator.

Example: -  ++ , -- , - (unary minus) , sizeof , ~ (compliment) , ! (not).

 

2.     Binary operators: - binary operators can be applied on two operands.

Example: -  + , -,  * , / , etc.

 

3.   Ternary operators: - Ternary operator can be applied on three operands.

Example: -  ?: (ternary conditional operator).

 


  • C supports several operators, can be classified into following categories.

1.      Arithmetic operators

2.      Relational operators

3.      Logical operators

4.      Assignment operators

5.      Bitwise Operators

6.      Increment/Decrement Operators

7.      Conditional operator

8.      Special operators

1)  Arithmetic operators: -

  • An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division and modulus on numerical values.
  • These operators can be applied on two or more variables or constants.
  • They work with both integer and floating-point numbers.

 

Operator

Name

Description

Example

(if a = 10,

b = 3)

Result

+

Addition

Adds two operands

a + b

13

-

Subtraction

Subtracts right operand from left

a - b

7

*

Multiplication

Multiplies two operands

a * b

30

/

Integer Division

Divides left operand by right (quotient)

a / b

3

Floating Division

Divides left operand by right (quotient)

(float) a / b

3.3333

%

Modulus

Returns remainder after division

a % b

1

 

Example: -                                                                                                     


Output: -


 2)  Relational operators: -

  • Relational operators are used to compare the values of two variables or constants.
  • These operators check the relationship between two operands. If the relation is true, it returns 1; if the relation is false, it returns the value 0.

          Returns 1 → if the condition is true

  Returns 0 → if the condition is false

 

  • C doesn't have a built-in boolean data type. Instead, it uses integers. Any non-zero value is considered true, and 0 is considered false.
  • These operators are widely used in Decision making ( if ) and Loops (for , while) in C programming.
  • The relational operators are:

 

Operator

Name

Description

Example

(if a = 2, b = 5)

Result

==

Equal to

Returns true if both are equal

a == b

0 (false)

!=

Not equal to

Returns true if not equal

a != b

1 (true)

> 

Greater than

True if left > right

a > b

0 (false)

< 

Less than

True if left < right

a < b

1 (true)

>=

Greater than or equal to

True if left ≥ right

a >= b

0 (false)

<=

Less than or equal to

True if left ≤ right

a <= b

1 (true)

 

Example: -                                                                                                    


output: -

 

3)  Logical operators: -

  • Logical operators are used to combine expressions or combine more than one conditions containing relational operators.
  • They are essential in decision-making (if, else, while, for) to test multiple conditions at once, for creating more complex conditions in programs.
  • The result of a logical operation is:

1 → if the final condition is true

0 → if the final condition is false

 

Operator

Name

Description

Example

(if a = 2, b = 5)

Result

&&


Logical AND

Returns true if both conditions are true

(a > 0 && b > 0)

1

||


Logical OR

Returns false if both conditions are false

(a > 2 || b > 6)

0

!

Logical NOT

Reverses the result

(true → false, false → true)

!(a > b)

1

 

Example: -

Output: -



4)  Assignment operators: -

  • Assignment Operators are used to assign values to a variable.
  • The basic assignment operator is denoted by equal to sign (=).
  • It is binary operator which operates on two operands. It has Two Values – Left-Value and Right-Value. Operator copies Right-Value into Left-Value.
  • C also provides compound assignment operators (like +=, -=, etc.) that combine arithmetic operations with assignment.
  • The assignment operators are:

 

Operator

Name

Description

Example

Meaning

=

Basic

assignment

Assigns right variable value to left variable.

a = b

Assigns value of b to a

+=

Addition assignment

Adds the right operand to the left operand and assigns the result to the left operand.

a += b

a = a + b

-=

Subtraction assignment

Subtracts the right operand from the left and assigns the result to the left operand.

a -= b

a = a - b

*=

Multiplication assignment

Multiplies the left operand by the right and assigns the result to the left operand.

a *= b

a = a * b

/=

Division assignment

Divides the left operand by the right and assigns the result to the left operand.

a /= b

a = a / b

%=

Modulus assignment

Calculates the modulus of the left operand with the right and assigns the result to the left operand.

a %= b

a = a % b

 

Example: -                                                                                  


output: -

 



5)  Bitwise Operators: -

  • Bitwise operators are used to perform operations on individual bits of integer data types.
  • They are very essential and mostly used in low-level programming, such as embedded systems, hardware control, data compression, cryptography, graphics, and optimizing performance.
  • These operators work on the binary representation (bits) of integers (e.g., int, char) and manipulate individual bits (0s and 1s).

 Note: Bitwise operators cannot be used with float or double.

 

Operator

Name

Description

Example

(a=5=0101, b=3=0011)

Result (in decimal)

&

Bitwise AND

1 if both bits are 1

a & b = 0101 & 0011 = 0001

1

|

Bitwise OR

1 if at least one bit is 1

a | b = 0101 | 0011 = 0111

7

^

Bitwise XOR

1 if bits are different

a ^ b = 0101 ^ 0011 = 0110

6

~

Bitwise NOT

Flips all bits (1→0, 0→1) add +1 to given bits

~a = ~0101 = 1010 (in 2’s complement = -6)

-6

<< 

Left Shift

Shifts bits left, adds 0s at right

a << 1 = 0101 → 1010

10

>> 

Right Shift

Shifts bits right, adds 0s at left (for positive numbers)

a >> 1 = 0101 → 0010

2

 



 Example: -



output: -


 

 6)  Increment/Decrement Operators: -

  • The increment (++) and decrement (--) operators are unary operators in C, used to increase or decrease the value of a variable by 1.
  • They are widely used in loops, counters, and array indexing.

 

a)     Increment operator (++): -

    • Increment operator is a unary operator which increases a variable value by 1 and stores it in same variable.
    • There are two increment operators: -

                                              i.          Pre-increment

                                             ii.          Post increment

 

i)    Pre-increment: -

    • Increment operator is placed before the variable or preceded by the variable.
    • First, increment operator is applied on to the variable and then the value is passed to second variable.

                                


              Example: -

                  


                   Output: -

 

 

ii)  Post increment: -

    • Increment operator is placed after the variable or next to variable.
    • First, the value in variable is passed to second variable then the first variable gets incremented.

 


 

Example: -                       


                                                                

output: -

 


b)    Decrement operator: -

    • Decrement operator is a unary operator which decreases a variable value by 1 and stores it in same variable.
    • There are two decrement operators: -

                                          i.        Pre-decrement operator

                                       ii.        Post decrement operator

 

i)    Pre-decrement operator: -

    • Decrement operator is placed before the variable or preceded by the variable.
    • First, decrement operator is applied on to the variable and then the value is passed to second variable.


            Example: -                                                                                                   


          output: -

 


ii)  Post decrement operator: -

    • Decrement operator is placed after the variable or next to variable.
    • First, the value in variable is passed to second variable then the first variable gets decremented.



Example: -                                                           


output: -

 


7)  Conditional operator: -

  • The conditional operator is also known as the ternary operator.
  • It is the only operator that takes three operands.
  • It provides a short form of writing simple if-else statements in a single line.
  • It is used to make quick decisions and assign values based on a condition.

 

Syntax: - The general syntax is:

 

condition ? expression1 : expression2;

 

·        condition: An expression that evaluates to either true (non-zero) or false (zero).

·        Expression1: If condition is true (non-zero), then expression1 is evaluated and returned.

·        Expression2: If condition is false (zero), then expression2 is evaluated and returned.

Example: - 


output: -

 

8)  Special operators: -

  • special operators are the operators which have special meaning and are mainly used for specific purposes such as referencing, accessing, and memory management.
  • The most frequently used special operators are:

                 a. sizeof operator

                b. comma operator

                c. pointer operator

 

a) Sizeof: -

sizeof operator returns size of a variable or type in bytes. 

Example: -


output: - 



b) Comma Operator ( , ): -

  • Allows two or more expressions to be evaluated in a single statement.
  • The last expression is the final result.

 Example: -


output: -

c) Pointer Operators ( * and & ): -

  • & (Address-of) operator gives the memory address of a variable.
  • (Dereference) operator gives the value stored at a memory location.

 Example: -


output: -



Popular posts from this blog

Variables in c

Cloud Storage and Local Storage: Applications in Business