OCJP

Postfix and Prefix Operators in C

In C language, prefix operators are those which are placed before operand ex. ++x, and same way postfix operators are those which are placed after operand ex. x++.

Main Difference is the TIME When Process of Increment or Decrements will be done.

Case A:
int x=20;
printf("%d ",++x);
printf("After :%d",x);

Will output like   21 After 21

Case B:
int x=20;
printf("%d ",x++);
printf("After :%d",x);

Output will be like  20 After 21

In case A Prefix ++ is used so value will be increased before printing the value of x, so first and second printf both will print 21.

In case B Postfix ++ is used, here In first printf statement First Value of X will be printed then it will be increased. So that First Printf will Print 20.

#include<stdio.h>
#include<conio.h>

void main()
{
	int x=20;
	int y=10,z;
	clrscr();

	printf("%d %d %d",x++,x++,x++); //22 21 20
	printf("\nNow %d",x); //23

	x=20;
	printf("\n%d %d %d",++x,++x,++x); //23 22 21
	printf("\nNow X %d",x);//23

	x=20;
	y=10;

	z= ++x + y++;
	printf("\nX %d Y %d and Z is %d",x,y,z);//21  11 31
	getch();


}
Prefix and Postfix Operators in C Language

During Printf Execution is done from Right To Left So x++ x++ x++ will process last x++ .

If you want more explanation or has any queries feel free to contact us using Comment Section.

Leave a Reply

Your email address will not be published. Required fields are marked *


× How can I help you?