C tutorial - Operators in C programming language

Programming languages or concepts

Operators in the C Programming Language:


Operators in C are symbols that perform various logical and mathematical operations on operands. They can be classified into different categories based on their functionality.


Operators:-

     An operator is a symbol that operates on a value or a variable.
    Operators is used to perform specific mathematical and logical computation on operands.
  Ex. Consider the below statement:
                 C= A+B;
 Here, + is the addition operator and A & B are operands. The addition operator tells  the compiler to add both of the operands A & B.
 And result stored in C operand.

Precedence operators:

       operator precedence determines the grouping of terms in an expression and decide how an expression is evaluated.
     Operators with the highest precedence at per at Top and those with the lowest actor at the bottom.
    Within an expression higher precedence operators will be executed first.

 

Category

Operator

Associativity

Postfix

() [] -> . ++ - -

Left to right

Unary

+ - ! ~ ++ - - (type)* & sizeof

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

Shift

<< >>

Left to right

Relational

< <= > >=

Left to right

Equality

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= += -= *= /= %=>>= <<= &= ^= |=

Right to left

Comma

,

Left to right



In above table top operator precedence is high and bottom position operator precedence is lowest.

Simple program for operators precedence:-

#include<stdio.h>

main ( )
{
       int a,b,c,d, result;
       printf ("Enter Integers numbers");
       scanf(" %d%d%d%d",&a,&b,&c,&d);
       result= (a+b) * c / d;
       printf ("Value of (a+b) * c / d: %d \n", result);
       
        return 0;
}

For example, consider a=20, b=10, c=15, d=5;
           result = (a +b) * c /d;
           result = ( 20 + 10) * 15 / 5 
                       = (30)* 15 /5
                        = 450 / 5
            result = 90.



C / C++ has many built in operators types this are classified as followed.

                 Types of Operators:-

1) Arithmetic operators.
2) Relational operators.
3) Logical operators.
4) Bitwise operators.
5) Assignment operators.
6) Other operators.
                a. Sizeof operators.
                 b. Comma operators.
                 c. Conditional operators.  
7) increment and decrement operators


 Arithmetic operators:-
      
Arithmetic operators are used for mathematical computations on numerical values.

- Addition (+): Adds two operands.
- Subtraction (-): Subtracts the second operand from the first operand.
- Multiplication (*): Multiplies two operands.
- Division (/): Divides the first operand by the second operand.
- Modulo (%) or remainder operator: Returns the remainder after dividing the first operand by the second operand.

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

Arithmetic operator are of two types:

1) Unary Operators:-
     Operators that operates or works with a single operand are Unary Operators.
  For example (++,--)
2) Binary Operators:-
     Operators that or works with a two operands are Binary Operators.
For example; (+,-,*,/)   

Learn Arithmetic operators:-

1) Addition operator :- the '+' operators adds two operands.
Ex. a+b;

2) Subtraction:- The '-' operator Subtracts two operands
Ex. A-B;
3) Multiplication:- The '*' operator multiplies two operands.
    Ex. A * B;
4) Division: the '/' operator divides the first operands by the second.
 Ex. X / Y;
5) Modules:- the '%' operator return the remainder when first operands is divided by the second.
Ex. X%Y;

 Relational Operators:-
   
Relational operators compare the values of two operands and return a boolean result (1 for true and 0 for false).

- Equal to (==): Checks if two operands are equal.
- Not equal to (!=): Checks if two operands are not equal.
- Greater than (>): Checks if the first operand is greater than the second operand.
- Less than (<): Checks if the first operand is less than the second operand.
- Greater than or equal to (>=): Checks if the first operand is greater than or equal to the second operand.
- Less than or equal to (<=): Checks if the first operand is less than or equal to the second operand.

Example:

int x = 5;
int y = 3;
int result1 = (x == y); // 0 (false)
int result2 = (x != y); // 1 (true)
int result3 = (x > y); // 1 (true)
int result4 = (x < y); // 0 (false)
int result5 = (x >= y); // 1 (true)
int result6 = (x <= y); // 0 (false)

 Logical Operators:

Logical operators are used to combine or manipulate boolean values.

- Logical AND (&&): Returns true if both operands are true.
- Logical OR (||): Returns true if either of the operands is true.
- Logical NOT (!): Negates the value of the operand.

Example:

int a = 5;
int b = 3;
int c = 7;

int result1 = (a > b) && (b < c); // 1 (true)
int result2 = (a > b) || (b > c); // 1 (true)
int result3 = !(a > b); // 0 (false)


 Bitwise Operators:

Bitwise operators perform operations on individual bits of integer operands.

- Bitwise AND (&): Performs a bitwise AND operation between the bits of two operands.
- Bitwise OR (|): Performs a bitwise OR operation between the bits of two operands.
- Bitwise XOR (^): Performs a bitwise exclusive OR operation between the bits of two operands.
- Bitwise NOT (~): Flips the bits of the operand, resulting in the one's complement.
- Left Shift (<<): Shifts the bits of the left 


Assignment operators:-

    Assignment operators are used to assign value to a variable.

    Different types of assignment operators are shown below.

Operator

Example

Same as

=

a = b

a = b

+=

a += b

a = a+b

-=

a -= b

a = a-b

*=

a *= b

a = a*b

/=

a /= b

a = a/b

%=

a %= b

a = a%b


    Other Operators:-

      a. Comma Operator:-
                 Comma Operator are used to link related expression together.
    Ex.
            int a , c = 5 , d;

        b. The sizeof operator:-
           The sizeof operator is Unary Operator that return the size of data.


   c.Conditional Operators:-

    Conditional operators are known by two more names
1. Ternary operator.
2. ? : Operator.
        It is used in if statement.
 

Syntax:

     expression 1 ? expression 2 : expression 3

Explanation:- 

      The question Mark ? in the syntax represent the if part. 
       The first expression (expression 1) generally return either true or false, based on which it is decided whether (expression 2) will be executed or (expression 3).
  If (expression 1) return true then the expression on the left side of ' : ' .
   If (expression 1) return false then the expression on the right side of ' : '.
i.e. (expression 3) is executed.    
     


Increment and decrement operators:-

    C has a special operator like increment decrement it called unary operators.
   These operators increment and decrement value of a variable by 1.
Increment and decrement operator can be used only with variables. They can't used with constant.
Ex 
       ++x is same as x=x+1
       --x  is same as x= x-1

Increment and decrement operator are are of two types.
           1). Prefix increment/decrement operator
           2). Postfix increment/decrement operator

1). Prefix increment/decrement operator:-

        In a prefix immediately increase or decrease the current value of the variable.
Ex.
         y = ++x

Here, firstly the current value of x is incremented by 1, the new value of x is then assign to y.
Similarly in following example of requirement first decrease value of x and then assign a new value of x to y.


int main()
{
    int x = 20, y = 10;

    printf("Initial value of x = %d\n"\n",x);
    printf("Initial value of y = %d\n\n", y); 

    y = ++x; // increment the value of x by 1 then assign this new value to y

    printf("After incrementing by 1: x = %d\n", x);
    printf("y = %d\n\n", y);

    y = --x; // decrement the value of x by 1 then assign this new value to y

    printf("After decrementing by 1: x = %d\n", x);
    printf("y = %d\n\n", y);


    return 0;
}


2). Postfix increment and decrement operators:-

         Causes  the current value of the variable to be used in the expression.
Ex.
      y = x++;
     the current value of x is assigned to y then is incremented by 1

    y = x - -;
         Current value of x is assigned to y then X is decremented by 1



close