Home > Article > Backend Development > In C language, array post-increment and front-increment
Use a C program to explain the concepts of post-increment and pre-increment of an array.
Increment operator ( ) -
is used to increase the value of a variable by 1
There are two types of increment operators - pre-increment and post-increment.
In prepended increment, the increment operator is placed before the operand, the value is incremented first, and then the operation is performed.
eg: z = ++a; a= a+1 z=a
The auto-increment operator is placed after the operand in the post-increment operation, and the value will be increased after the operation is completed.
eg: z = a++; z=a a= a+1
Let us consider an example of accessing a specific element in a memory location by using pre-increment and post-increment.
Declare an array of size 5 and perform compile-time initialization. Afterwards try assigning the pre-increment value to variable 'a'.
a=++arr[1] // arr[1]=arr[1]+1 a=arr[1] b=arr[1]++// b=arr[1] arr[1]+1
Demonstration
#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; }
4--3--25
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
Consider another example to learn more about pre-increment and post-increment of an array.
Real-time demonstration
#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; }
27--26—0
The above is the detailed content of In C language, array post-increment and front-increment. For more information, please follow other related articles on the PHP Chinese website!