Difference between pre-increment and post-increment operator in C.

If you're not part of the solution, you're part of the precipitate. -Henry J. Tillman
Greetings, today we are going to find out the difference between the pre-increment(++a) and post-increment(a++) operator in C- as the title says. The best way to learn anything is by example, so here it is,

Example 1:- 
//Pre-increment operator
#include <stdio.h>
 
int main(void) {
int a=3;
 
printf("%d",++a);
return 0;
}
Output: 4

//Post-increment
#include <stdio.h>

int main(void) {
 int a=3;
 
 printf("%d",a++);
 return 0;
}
Output: 3

Explanation: As you can see there is difference in the output. The
printf("%d",a++) is composed of two statements:

printf("%d",a);
a=a+1;

In pre-increment,  a=a+1; is evaluated before printf("%d,"a);

In post-increment,  printf ("%d",a); is evaluated  before a=a+1;

Comments

Popular posts from this blog

Solution of Codeforces Round #244 (Div. 2) - Police Recruits in C.

Solution of Codechef - May Challenge 2014 - Chef Jumping in C