03.05. Understanding Prefix and Postfix – StudyEasy Organisation (2024)

Increment and decrement operators have 2 variation or types

  1. prefix
  2. postfix

In prefix, operators are written before their operands.

Example:++10

In postfix, operators are written after their operands.

Example: 10++

Now we have already seen that 10++ and ++10 both will give us 11, then what’s the difference?

What will be the output?

C

1

2

3

int x = 10;

printf(%d,++x);

The output will be

Again what will be the output?

C

1

2

3

int x = 10;

printf(%d,x++);

Strangely the output will be

C

1

10

What happened actually?

In case 1, the value of x is incremented first and then the value 11 got printed, whereas in case 2, the value got printed first and the value incremented. So do you now understood the difference?

In prefix, the value of the variable will get incremented first and then the statement will get executed. Whereas in postfix, the statement will be executed with the current value of a variable and then the value gets incremented.

Let’s see these programs

Program: Postfix

C

1

2

3

4

5

6

7

8

9

10

11

/**********

Postfix

**********/

#include<stdio.h>

main()

{ .

int x = 1;

printf("%d\n" ,x++);

printf("%d",x);

getch();

}

Output:

C

1

2

1

2

Program: Prefix

C

1

2

3

4

5

6

7

8

9

10

11

/**********

Prefix

**********/

#include<stdio.h>

main()

{ .

int x = 1;

printf("%d\n" ,++x);

printf("%d",x);

getch();

}

Output:

C

1

2

2

2

So observing the output of the program, we can understand the difference between postfix and prefix.

Contributed by:Salim Sheikh

I'm an enthusiast with a solid background in C programming, and I've delved into the intricacies of increment and decrement operators. My expertise in this area allows me to provide a detailed analysis of the concepts discussed in the article you provided by Salim Sheikh.

In the article, Salim explores two variations of increment and decrement operators: prefix and postfix. These operators play a crucial role in C programming, influencing the order of execution and output in different scenarios.

Let's break down the key concepts mentioned in the article:

  1. Prefix Increment Operator (++x):

    • The prefix increment operator is written before the operand (e.g., ++10).
    • It increments the value of the variable first and then executes the statement.
    • Example:
      int x = 10;
      printf("%d", ++x);
      // Output: 11
  2. Postfix Increment Operator (x++):

    • The postfix increment operator is written after the operand (e.g., 10++).
    • It executes the statement with the current value of the variable and then increments the value.
    • Example:
      int x = 10;
      printf("%d", x++);
      // Output: 10
  3. Difference Between Prefix and Postfix:

    • The article highlights a crucial difference in output based on the order of execution.

    • Example:

      int x = 10;
      printf("%d", ++x); // Output: 11
      printf("%d", x++); // Output: 10
    • In prefix, the value of the variable is incremented first, and then the statement is executed.

    • In postfix, the statement is executed with the current value of the variable, and then the value gets incremented.

  4. Program Demonstrations:

    • Salim provides two program examples to illustrate the difference between postfix and prefix:

      • Postfix Program:

        #include<stdio.h>
        int main(){
        int x = 1;
        printf("%d\n", x++);
        printf("%d", x);
        getch();
        }

        Output: 12

      • Prefix Program:

        #include<stdio.h>
        int main(){
        int x = 1;
        printf("%d\n", ++x);
        printf("%d", x);
        getch();
        }

        Output: 22

By thoroughly understanding the nuances of prefix and postfix operators, developers can optimize their code and avoid unexpected outcomes. Salim Sheikh's article effectively conveys these concepts through practical examples. If you have any specific questions or if there's a particular aspect you'd like to delve into further, feel free to ask.

03.05. Understanding Prefix and Postfix – StudyEasy Organisation (2024)
Top Articles
Latest Posts
Article information

Author: Wyatt Volkman LLD

Last Updated:

Views: 6561

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Wyatt Volkman LLD

Birthday: 1992-02-16

Address: Suite 851 78549 Lubowitz Well, Wardside, TX 98080-8615

Phone: +67618977178100

Job: Manufacturing Director

Hobby: Running, Mountaineering, Inline skating, Writing, Baton twirling, Computer programming, Stone skipping

Introduction: My name is Wyatt Volkman LLD, I am a handsome, rich, comfortable, lively, zealous, graceful, gifted person who loves writing and wants to share my knowledge and understanding with you.