What is increment operators(i++) in C,It's Example and Use

If you want to know about the increment operator in c. So you must know what are the operators? If you do not know, then you know. So let's start.


First of all, know what is operator

An operator is a signal that is used to perform operations between any of the operands.Operators like +, -, *, / etc.


    What is an increment operator with an example?


    The increment operator is the power operator. It is a Unary operator.

    ++ is an increment operator. It adds value to the operant (variable). Which is called the increment of the variable.

    Increment operator in c


    Tell the increment operator in two parts

    1. Post-increment
    2. Pre-increment


    What is Post-Increment (i++)


    If the increment operator is placed after the operator ie variable, then it is called post-increment.

    With this increment, the value of the variable becomes +1.


    #include<stdio.h>

    void main ()

    {

    int c = 10;

    c++;

    printf ("Increment integer c %d",c);

    getch ();

    }


    What is Pre-Increment


    If the increment operator ++ is placed before the operator variable like ++ c then it is called pre-increment.

    In this, +1 is added to the value of the first operator ie variable. Then its value is used.


    #include<stdio.h>

    void main ()

    {

    int c = 10;

    ++c;

    printf ("Increment integer c %d",c);

    getch ();

    }


    Like we have seen that both types have a variable value of +1. But what is the difference between pre and post-increment? For which they are used for different work.


    What is difference between ++ i and i ++?.


    The biggest difference between the two is that of expression and assignment statement. Through this, we can know the difference between them.


    We will understand through the program what is the use of post-increment and what is the use of pre-increment.


    Post-increment (i++)


    #include<stdio.h>

    void main ()

    {

    int a = 10;

    int b;

    b = a++;

    printf ("value of a %d",a);

    printf ("value of b %d",b);

    getch ();

    }


    Post increment is used in this program. In which a = 10. And there is no value in b.

    When b = a ++; Let's do it.Then the value of a comes in b. That means b = 10 will be 11.

    The increment of a ++ will not come in b. The value of first a will come in b.


    Pre-increment (++i)


    #include<stdio.h>

    void main ()

    {

    int a = 10;

    int b;

    b = ++a;

    printf ("value of a %d",a);

    printf ("value of b %d",b);

    getch ();

    }


    Post increment is used in this program. In which a = 10. And there is no value in b.
    When b = ++ a; Let's do it.Then b does not have a.

    That is, the increment value of  ++a will come in b.

    Why use Increment Operator?


    This operator is a very useful operator. From which any value can be incremented. It is very useful in the use loop.


    Read More: 


    Post a Comment

    0 Comments