Overview of C Increment and Decrement Operator with Example

Engg.... | 01:24 | | | | Best Blogger Tips
Increment operator
Increment operator:
  • var ++ => Indicates post increment. That is use the value of var first and then increment.
  • ++ var => indicates pre increment.That is the value of var must be incremented before it is used. 
 Example:
 
#include<stdio.h>
void main()
{
int x , y;
x=5;
y=10;
printf(" x=%d\n, x++");
printf(" y=%d\n, ++y");
}

Output:
  x=5
  y=11


Decrement operator:
  • --var => indicates decrement the value of var first and then use it.
  • var-- => indicates use the value of var first and then decrement it.
Example:
#include<stdio.h>
void main()
{
int x , y;
x=5;
y=10;
printf(" x=%d\n, --x");
printf(" y=%d\n, y--");

Output:
  x=4
  y=10



Please Share it! :)
Do you like this Story?

Get Free Email Updates Daily!

Follow us!

22 comments:

  1. #include
    void main();
    {
    int a,b,c;
    a= 5;
    b= ++a;
    c= a++;
    printf("%d%d%d",a,b,c);
    getch();
    }

    ReplyDelete
  2. #include

    void main()
    {
    int b,c;
    c = 10;
    b = ++c + ++c;
    printf("%d%d",b,c);
    }

    ReplyDelete
  3. #include
    main()
    {
    int a;
    int b;
    int c;
    b=++a + ++a + ++a + ++a;
    printf("a=%d b=%d\n",a,b);
    c=++a + ++a + ++a + ++a;
    printf("a=%d c=%d\n",a,c);
    }

    ./aout a=5 b=4
    a=9 b=31

    ReplyDelete
    Replies
    1. Thank you so much for asking This question ..it Completely Cleared my concepts on Increment/decrement operator.
      If we take a=5
      the answere will be

      a=9 b=36

      a=13 b=52

      EXPLANATION:
      In first line b=++a + ++a + ++a + ++a
      there are 4 prefix operator .
      In prefix operator=First increment then Evaluation takes place.So
      a=5
      ++a=6
      again
      ++a=7
      agin then
      ++a=8
      again there is ++a
      so ++a=9 ..whenever we have the equation like this first we calculate the prefix vale and then write the equation like this
      b=a+a+a+a
      so final value of a=9
      b=9+9+9+9=36

      same process will be for c but now the value of a is 9.
      Thank you

      Delete
    2. THIS IS WRONG ASN b=9+9+9+9=36

      RIGHT ANS b=10+11+12+13=46

      Delete
    3. a=4 b=11
      a=8 c=27
      the correct answer

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. write code in C for cumulative product from 1-10

    ReplyDelete
  6. what will be the output?
    #include
    int main(){
    printf("%d","abcde"-"abcde");
    return 0;
    }

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
  7. Nice tutorial. Thanks for sharing the valuable info about c Training. it’s really helpful. Who want to learn c language this blog most helpful. Keep sharing on updated tutorials…..

    ReplyDelete