Skip to content
Related Articles

Related Articles

Precedence of postfix ++ and prefix ++ in C/C++

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 14 Jun, 2022

In C/C++, precedence of Prefix ++ (or Prefix –) has same priority than dereference (*) operator, and precedence of Postfix ++ (or Postfix –) is higher than both Prefix ++ and *. 
If p is a pointer then *p++ is equivalent to *(p++) and ++*p is equivalent to ++(*p) (both Prefix ++ and * are right associative).
For example, program 1 prints ‘h’ and program 2 prints ‘e’.
 

C++




// Program 1
// C++ program to explain the precedence of 'Prefix ++'
 
#include <iostream>
using namespace std;
 
int main()
{
  char arr[] = "geeksforgeeks";
  char *p = arr;
  ++*p;
  cout << *p;
  return 0;
}
 
// This code is contributed by sarajadhav12052009


C




// Program 1
#include<stdio.h>
int main()
{
  char arr[] = "geeksforgeeks";
  char *p = arr;
  ++*p;
  printf(" %c", *p);
  getchar();
  return 0;
}


Output

h

 

C++




// Program 2
// C++ Program to explain the precedence of 'Postfix ++'
 
#include <iostream>
using namespace std;
 
int main()
{
  char arr[] = "geeksforgeeks";
  char *p = arr;
  *p++;
  cout << *p;
 
  return 0;
}
 
// This code is contributed by sarajadhav12052009


C




// Program 2
#include<stdio.h>
int main()
{
  char arr[] = "geeksforgeeks";
  char *p = arr;
  *p++;
  printf(" %c", *p);
  getchar();
  return 0;
}


Output

e

Program to tell a person’s surname

C++




// C++ program that tells a person's last name
 
#include <iostream>
using namespace std;
 
int main()
{
  string fullName[] = {"Joe", "Donaldson"};
  string *ptr = fullName;
   
  cout << *ptr << "'s Last Name is ";
  *ptr++;
  cout << *ptr << endl;
}
 
// This code is contributed by sarajadhav12052009


Output

Joe's Last Name is Donaldson

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!