Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Operators in C Part 1

Operators are symbols used to perform certain operations on the data. There are basically 5 types of operators :
  1. Arithmetic Operators
  2. Assignment Operators
  3. Comparision Operators
  4. Logical Operators
  5. Bitwise Operators
Today, we are going to look at arithmetic and assignment operators.

Arithmetic Operators

They are used to perform mathematical operations on values. They are of two types :
  1. Binary Operators
  2. Unary Operators

Binary Operators

These are the operators which work on at least two operands(or numbers). For example, you need addition, multiplication, division, etc.For example.

    #include <stdio.h>
    void main()
    {
        int a=5;
        int b=10;
        int c=a+b;
        int d=a-b;

        printf("%d + %d = %d ",a,b,c);
        printf("%d - %d = %d ",a,b,d);

        //Or, we can simply do it without creating any variables.
        printf("%d X %d = %d ",a,b,a*b);
        printf("%d / %d = %d ",b,a,b/a);
    }            
    

Output

    5 + 10 = 15
    5 - 10 = -5
    5 * 10 = 50
    10 / 5 = 2
        

Unary Operators

They work on only one operand(or number). They are majorly of 4 types -
  1. Pre-increment - First increase the value by 1 and then prints it.
  2. Pre-decrement - First decrease the value by 1 and then prints it.
  3. Post-increment - First prints the value and then increases it by 1.
  4. Post-decrement - First prints the value and then decreases it by 1.

    #include
    void main()
    {
    int a=4;
    printf("Value of a = %d",a);
    printf("Value of ++a = %d",++a);     // a becomes 5
    printf("Value of --a = %d",--a);     // a becomes 4
    printf("Value of a++ = %d",a++);     // a becomes 5 but prints 4
    printf("Value of a = %d",a);       // Current value of a is 5
    printf("Value of a-- = %d",a--);     //Now a is 5 so print 5 and then decrement
    printf("Value of a = %d",a);       // Current value of a is 4
    }

Output

    Value of a = 4
    Value of ++a = 5
    Value of --a = 4
    Value of a++ = 4
    Value of a = 5
    Value of a-- = 5
    Value of a = 4
    

Assignment Operators

The (=) operator is used to assign values to variables. For example -

    #include <stdio.h>
    void main()
    {
    int a=4;
    printf("Value of a = %d",a);
    //Make b equal to a
    int b=a;
    printf("Value of b = %d",b);
    b=7;
    //Value of a remains unchanged
    printf("Value of a = %d",a);
    printf("Value of b = %d",b);
    }
    

Output

    Value of a = 4
    Value of b = 4
    Value of a = 4
    Value of b = 7
        
It is of 4 types as follows :
  1. Addition Assignment : a+=2; (Same as a=a+2;)
  2. Subtraction Assignment : a-=2; (Same as a=a-2;)
  3. Multiplication Assignment : a*=2; (Same as a=a*2;)
  4. Division Assignment : a/=2; (Same as a=a/2;)

    #include <stdio.h>
    void main()
    {   
    int a;
    printf("Enter the value of a :");
    scanf("%d",&a);
    a+=2;   
    printf("Value of a after adding 2 = %d ",a);
    }   

Output

    Enter the value of a:10
    Value of a after adding 2 = 12