×

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators to manipulate data and variables. The different types of operators are:

There are many operators used in the c language among them increment and decrement operators are used. The operand or variable which has the location in the memory for that operands only we can apply the increment operator.

Increment

In any c programming language for increasing an operand by 1, we use the increment operator. Increment operator represented by ” ++”. Most widely these operators are used in for and while loops.

The increment operator uses only one operand so that is why they are the unary operators. Increments operator is of two types they are pre-increment and post-increment.

  • Pre increments:
    Incremented and then assigned i.e, the operand value is incremented by 1 before the value is assigned to the operand. Represented by ++operator.
    In simple, we can say that change and use;
    • Change the value of a variable.
    • Use the new value.
  • Post increments:
    Assigned and then incremented i.e, the operand value is incremented by 1 after the value assigned to the operand. Represented by operator++.
    In simple, we can say that use and change;
    • Use the original value of a variable.
    • Change the value of a variable.

When statements are formed independently then  ++operator and operator++ mean the same thing, when they are used in expressions on the right side of an assignment statement they behave differently.

For example:

Consider a=6;
         b=++a;

From the above example, the value of b and a is 7 because it follows the increment and then assigns i.e,first  1was added to the a, and then it assigned the result to the b on left in the pre-increment.

 a=6;
 b=a++;

from the above case, the value of b is 6, and a will be 7 because it follows assign and then increment i.e, assigned the value to the b on the left first, and then a value is incrementedin the post-increment.

In subscripted variables when we use the increment operator i.e,++, the statement   a[i++]=10;   is equivalent to

a[j]=10;

j=j+1;

even using pre or post forms the value cannot be changed for the stand-alone operands.

Simple Programs for pre-increment and post-increment

Pre-increment:

#include<stdio.h>
Int main()
{
Int i=10,j;// vaiable declaration
j=++i;//pre-increment
printf(“i=%d\n”,i);
printf(“j=%d\n”,j);
return 0;
}

Output:               

i=10
j=10

Post-increment:

#include<stdio.h>
Int main()
{
Int i=10,j;// vaiable declaration
j=i++;//post-increment
printf(“i=%d\n”,i);
printf(“j=%d\n”,j);
return 0;
}

Output:     

i=11
j=10

Important Points

  • When comparing post-increment and pre-increment, pre-incrementis faster this is because post-increment remains a copy of the old value and adds one to the existing value while pre-increment simply adds on without keeping the existing value.
  • These operators are mostly used in the loops and in arrays to increase the index values.
  • Compare to addition operations increment operations are efficient.
  • In the case of n++ number operations are performed by the CPU in its Machine code compared to the n=n+1.
  • They are applied only to the variables. And not used with the boolean values, not allowed the nesting of operators.
  • In the precedence table the pre-increment is in the first row and then the second row is post-increment.

Examples of pre-increment and post-increment using loops

For loop

#include<stdio.h>
int main()
{
int number;     //for loop to print 1-10 numbers
for(number=1;number<=10;number++)	// post-increment
{
printf("%d\t",number);	//to print the number
}
return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10

For loops produce identical results even if we use post-increment and the other pre-increment.

Examples of pre-increment and post-increment using an array

#include<stdio.h>
Int main()
{
int x,y,z;
int a[5]={1,2,3,4,5};
x=++a[1];// pre-increment
y=a[1]++;// post-increment
z=a[x++];
printf(“%d\n%d\n%d\n”,x,y,z);
return 0;
}

The above example is easy to solve but may get the wrong solution sometimes.so we need to understand it properly. Before that, there is an important point to be noted that is mentioned above. i.e

  • The pre-increment is a higher precedence than the post-increment

Understanding the example:

First step :x = ++a[1];   // if a[1]=2 then it will be incremented and stored in x.

                       x and a[1] is 3.

second step :y=a[1]++;

                        This line is a tricky part of the program.

/*because of post-increment, first, y is assigned with a[1] and then incremented and stored in a[1].*/

a[1] value is 4, and y value is 3.

Third step :z =a[x++];

//because x is post-increment, the x value is used first and incremented later.

 So, a[3] is stored z.


Related Topics

Control statement in C

What is a control statement? A control statement helps us to control the flow of the program. The control statement helps us to execute the program's instructions in a user-defined order....

8 minutes read.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

Execution flow of C program

There are various steps of the execution of the C program that is given below. The following step of the execution Write source codes Preprocess Compile Link edit Load Execute Editor or...

1 minute read.

Nested Loops in C Programming Examples

A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a “loop inside the...

6 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

C Program to Find the Largest Number using Ternary Operator

In this tutorial, we will write some program to determine which of the provided integers is larger using the ternary operator, also known as the conditional operator in C. Ternary Operator: The ternary...

3 minutes read.

Integer Promotions in C

As we know that some of the data types such as char, short int, Enum takes a smaller number of bytes than compared to int. When an operation is applied...

3 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

#undef in C

#undef in C In the C programming language, the #undef directive is used to undefine the macro or any constant, which will be defined by the preprocessor directive #define. The #undef...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Array to function in C

Array to function in C We can create functions that receive an array as an argument. To pass an array into a function, we need to write the name of the...

4 minutes read.

If Statement in C

Decision Control Statements We frequently desire one set of instructions to be carried out in one circumstance while another set of instructions to be carried out in a completely different circumstance....

3 minutes read.

Float in islower() in C

Introduction: This article briefly discusses the float in islower() in C. The islower() function checks if the input character passed inside the function is lowercase. Lowercase letters include (a-z). The islower()...

4 minutes read.

Palindrome Number in C

What is a Palindrome? A number that doesn't change when reversed is known as a palindrome. We reverse a number and compare it to the original number to determine whether it is...

4 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

What is 2s Complement in C?

The complement of 2s in C is generated from the 1s in C. As we know, the 1s complement of a binary number is generated by converting bit 1 to...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.