Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Precedence
3.
Code
4.
Code
5.
FAQs
5.1.
What are prefix ++ and postfix ++?
5.2.
What is the difference between prefix ++ and postfix ++?
6.
Conclusion 
Last Updated: Mar 27, 2024
Easy

Precedence of postfix ++ and prefix ++ in C

Author Nishant Rana
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

Postfix ++ and the prefix++ operators are the incrementing operators. They are used to increment the value of a variable by 1. Following is the main difference between them:-

  1. prefix ++: It increments the value of the variable as soon as it is encountered. It increments the variable's value before running the current line of code.
  2. postfix ++: It increments the variable’s value after running the current line of code.
     

Now, let us see how these operators behave in the case of pointers. Pointers are basically the objects which contains the memory address.

  1. ++ *ptr → ++(*ptr)
  2. *ptr++ → *(ptr++)

There is a difference in both operators. Hence, we need to be careful in the case of pointers.

Also See, Sum of Digits in C, C Static Function

Precedence

The postfix ++ or -- has a higher priority than the prefix ++ or -- and the deference operator ‘*’. The prefix ++ or -- has a higher priority than the deference operator ‘*’.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Code

#include<iostream>
using namespace std;
int main() {
   int arr[] = {5, 6, 9};
   int *ptr = arr;
   ++*ptr;
   cout << *ptr;
   return 0;
}

Output

6

Explanation

So here at first ptr is pointing to the value of the 0th index of arr which is 5. After using ++*ptr it increases 5 by 1, and now the value is 6.

Code

#include<iostream>
using namespace std;
int main() {
   int arr[] = {5, 6, 9};
   int *ptr = arr;
   cout << *ptr++;
   return 0;
}

Output

6

Explanation

So here at first ptr is pointing to the value of the 0th index of arr which is 5. But since we are applying the postfix ++, the ptr will still be at 0th index at the time of printing.

Also see,  Short int in C Programming

FAQs

What are prefix ++ and postfix ++?

Postfix ++ and the prefix++ operators are the incrementing operators. They are used to increment the value of a variable by 1.

What is the difference between prefix ++ and postfix ++?

Prefix ‘++’ increments the variable's value before running the current line of code whereas postfix ++ increments the variable’s value after running the current line of code.

Conclusion 

In this article, we have extensively discussed the following things:

  1. We first discussed the prefix ++ and postfix ++ operators.
  2. Then we discussed their precedence.

We hope that this blog has helped you enhance your knowledge regarding prefix ++ and the postfix ++ and if you would like to learn more, check out our articles here

Check out this problem - Longest Common Prefix

Do upvote our blog to help other ninjas grow. Happy Coding!


Previous article
operands for sizeof operator in C
Next article
Difference between ++*p, *p++ and *++p
Live masterclass