Increment Operators in C | Pre increment and Post Increment operators in C Language

Spread the love

Increment Operator :

  • The Increment operator increments the Value of the variable by 1(by adding 1 to it’s Current Value).
  • Increment Operator is Unary operator. That means Increment operator is operates on only one Operand.
  • Increment Operator have Highest priority than all Binary Operators. Because it is unary operator and Unary operators have higher priority than Binary operators. You can see Full priority table here C operators Priority table.
  • Increment Operator can not work on Constants. it can be apply only on Variables.
  • Increment Operator Denoted by ++ Symbol.
  • Ex : ++X  or X++.

Different Types of Increment operators :

We have two types of Increment operators those are
  1. pre-increment operator (or) prefix increment operator.
  2. post-increment operator (or) postfix increment operator.

Pre-incremet Operator :

  • In pre-increment operator, Operator is written before the operand.
  • Pre-increment operator Increments the value of variable first, then incremented value is used to evaluate the Expression.
Ex: 
  1.  a= 5;
  2.  y = ++a;
In above example y value will be 6. because a value is first incremented then it will assigned to y. so y value is 6.

Post-Increment Operator :

  • In post-increment operator, Operator is written after the Operand.
  • Post-increment operator first expression is Evaluated and then value of Variable is Incremented. that means Incremented value is not used in expression.
Ex:
  1.  a= 5;
  2.  y = a++;
in above example y value is 5. because in post increment operator value first assigned and then Incremented so value of y is 5.but value of a after that expression is 6.

Example program 1 : C program to Understand Increment operator

  1. #include<stdio.h>
  2. int main(void)
  3. {
  4.         int x=8,y=10;
  5.         printf(“x=%dt,x);
  6.         printf(“x=%dt,++x);   /*Prefix increment*/
  7.         printf(“x=%dt,x);
  8.        
  9.         printf(“y=%dt,y);
  10.         printf(“y=%dt,y++);   /*Postfix increment*/
  11.         printf(“y=%dt,y);
  12.         return 0;
  13. }
output :

Example 2 : program to understand Increment Operator in C

  1. #include<stdio.h>
  2. void main()
  3. {
  4. int a,b,c=5,d=5;
  5. = c++;
  6. = ++d;
  7. printf(“Value of a : %d”,a);
  8. printf(“Value of b : %d”,b);
  9. printf(“Value of c : %d”,c);
  10. printf(“Value of d : %d”,d);
  11. }
output :

Example 3 : Increment Operator can not be used on Constants

  1. #include<stdio.h>
  2. void main()
  3. {
  4.         int a;
  5.         a = 10++;
  6. }
output :
Compilation error time: 0 memory: 2248 signal:0

So it is clear that we can not use constants in increment operators.
Also Read :
  1. Modulo operator in C explained with Examples.
  2. In-depth examples on Arithmetic operators.
  3. Arithmetic operators in C language.
  4. Compilation Stages in C language.
  5. Identifiers in C language and Rules for naming Identifiers.
  6. Structure of C Language.

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer and an Open Source enthusiast, Presently working as a Software Engineer. Apart from that, I love to explore new technologies and things.

You may also like...

2 Responses

  1. Increase value vs address

    Increase value vs address

    #include

    int main(void)
    {
    int *p, j;

    p = &j;

    j = 1;
    printf("%p ", p);

    (*p)++; /* now j is incremented and p is unchanged */
    printf("%d %p", j, p);

    return 0;
    }

Leave a Reply