×

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

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.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

Fee Management System in C

The concept is to split every operation into its very own characteristic. Software is made from all of the capabilities mixed with transfer cases. An example of the capabilities may...

5 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Caesar Cipher Program in C

What is Caesar Cipher? The Caesar Cipher is a type of substitution cipher that is named after Julius Caesar, who is said to have used it to encrypt messages sent to...

2 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

Error handling in C

Error handling in C: The C programming standard does not provide direct convenience for handling the errors. However, being a system programming language, it definitely will give access to handling...

4 minutes read.

Nested loop in C

Definition of Nested Loop 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...

3 minutes read.

C Program Swap Numbers in cyclic order Using Call by Reference

The three integers that the user entered in cyclical sequence are swapped in this tutorial using call by reference. C Program: #include <stdio.h> void cyclic_Swap ( int *X, int *Y, int *Z ); int...

2 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

4 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

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.

Difference between If and Switch Statement in C

Key Contrast: In the event that assertion is utilizes a Boolean articulation to execute the capability and can frequently be utilized to really look at various circumstances all at once.  The change...

4 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

C Program to Find Largest Number Using Dynamic Memory Allocation

This tutorial will teach us how to locate the highest number a user has ever input into a dynamically allocated memory. C Program: #include <stdio.h> #include <stdlib.h> int main ()  {   int no;   double *data_n;   printf...

1 minute read.

Function in C

Function is a group of statements that are used to perform any task. In other words, we can say that a function is a self- contained a block of programs...

3 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.