# Post and Pre incremented of arrays in C language

## Problem

Explaining the array post and pre incremented concept with the help of C program.

## Solution

Increment operator (++) −

• It is used to increment the value of a variable by 1

• There two types of increment operators − pre increment and post increment.

• Increment operator is placed before the operand in preincrement and the value is first incremented and then operation is performed on it.

eg: z = ++a; a= a+1
z=a
• Increment operator is placed after the operand in post increment and the value is incremented after the operation is performed.

eg: z = a++; z=a
a= a+1

Let’s consider an example to access particular elements in memory locations by using pre- and post-increment.

Declare an array of size 5 and do compile time initialization. After that try to assign a pre incremented value to ‘a’ variable.

a=++arr[1] // arr[1]=arr[1]+1
a=arr[1]
b=arr[1]++// b=arr[1]
arr[1]+1

## Example 1

Live Demo

#include<stdio.h>
int main(){
int a, b, c;
int arr[5] = {1, 2, 3, 25, 7};
a = ++arr[1];
b = arr[1]++;
c = arr[a++];
printf("%d--%d--%d", a, b, c);
return 0;
}

## Output

4--3--25

## Explanation

here, a = ++arr[1]; i.e a = 3 //arr[2];
b = arr[1]++; i.e b = 3 //arr[2];
c = arr[a++]; i.e c = 25 //arr[4];
printf("%d--%d--%d",a, b, c);
printf("%d--%d--%d",4, 3, 25);
Thus 4--3--25 is outputted

## Example 2

Consider another example to know more about pre- and post-incremented of array.

Live Demo

#include<stdio.h>
int main(){
int a, b, c;
int arr[5] = {1, 2, 3, 25, 7};
a = ++arr[3];
b = arr[3]++;
c = arr[a++];
printf("%d--%d--%d", a, b, c);
return 0;
}

## Output

27--26—0