Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Post and Pre incremented of arrays in C language
In C programming, pre and post increment operators can be applied to array elements to modify their values. Understanding how these operators work with arrays is crucial for effective C programming.
Syntax
// Pre-increment: increment first, then use value variable = ++array[index]; // Post-increment: use value first, then increment variable = array[index]++;
Pre-increment vs Post-increment
Increment operator (++) −
- It is used to increment the value of a variable by 1
- There are two types of increment operators − pre increment and post increment
- Pre-increment (++variable): Increment operator is placed before the operand. The value is first incremented and then the operation is performed
- Post-increment (variable++): Increment operator is placed after the operand. The value is incremented after the operation is performed
Example 1: Basic Pre and Post Increment
Let's examine how pre and post increment operators work with array elements −
#include <stdio.h>
int main() {
int a, b, c;
int arr[5] = {1, 2, 3, 25, 7};
printf("Original array: ");
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("<br><br>");
a = ++arr[1]; /* Pre-increment: arr[1] becomes 3, then a = 3 */
printf("After a = ++arr[1]: a = %d, arr[1] = %d<br>", a, arr[1]);
b = arr[1]++; /* Post-increment: b = 3, then arr[1] becomes 4 */
printf("After b = arr[1]++: b = %d, arr[1] = %d<br>", b, arr[1]);
c = arr[a++]; /* Post-increment: c = arr[3] = 25, then a becomes 4 */
printf("After c = arr[a++]: c = %d, a = %d<br>", c, a);
printf("\nFinal result: %d--%d--%d<br>", a, b, c);
return 0;
}
Original array: 1 2 3 25 7 After a = ++arr[1]: a = 3, arr[1] = 3 After b = arr[1]++: b = 3, arr[1] = 4 After c = arr[a++]: c = 25, a = 4 Final result: 4--3--25
Example 2: Another Demonstration
Consider another example to understand the behavior with different array indices −
#include <stdio.h>
int main() {
int a, b, c;
int arr[5] = {1, 2, 3, 25, 7};
printf("Original array: ");
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("<br><br>");
a = ++arr[3]; /* Pre-increment: arr[3] becomes 26, then a = 26 */
printf("After a = ++arr[3]: a = %d, arr[3] = %d<br>", a, arr[3]);
b = arr[3]++; /* Post-increment: b = 26, then arr[3] becomes 27 */
printf("After b = arr[3]++: b = %d, arr[3] = %d<br>", b, arr[3]);
c = arr[a++]; /* Post-increment: c = arr[26] (out of bounds), then a becomes 27 */
printf("After c = arr[a++]: c = %d, a = %d<br>", c, a);
printf("\nFinal result: %d--%d--%d<br>", a, b, c);
return 0;
}
Original array: 1 2 3 25 7 After a = ++arr[3]: a = 26, arr[3] = 26 After b = arr[3]++: b = 26, arr[3] = 27 After c = arr[a++]: c = 0, a = 27 Final result: 27--26--0
Key Points
- Pre-increment: Value is incremented first, then used in the expression
- Post-increment: Current value is used in the expression, then incremented
- Be careful with array bounds − accessing
arr[26]when array size is 5 leads to undefined behavior - Both operators modify the original array element permanently
Conclusion
Pre and post increment operators with arrays follow the same rules as with regular variables. Understanding the timing of when the increment occurs is essential for writing correct C programs and avoiding unexpected results.
Advertisements
