
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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
#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.
#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
- Related Articles
- Pre-increment and Post-increment in C/C++
- Pre-increment and Post-increment concept in C/C++?
- Pre & post increment operator behavior in C, C++, Java, and C#
- Arrays in C Language
- Explain the pre-processor directives in C language
- Write a C program to demonstrate post increment and pre increment operators
- What are the Pre-processor Commands in C language?
- Explain the characteristics and operations of arrays in C language
- Explain the concepts of Pointers and arrays in C language
- How to use Pre-defined mathematical function in C language?
- How to merge to arrays in C language?
- Pre-increment (or pre-decrement) in C
- How to perform the arithmetic operations on arrays in C language?
- Post Modernism, Post Structuralism and Post Colonialism
- How to swap two arrays without using temporary variable in C language?
