Precedence of postfix ++ and prefix ++ in C/C++
Last Updated :
03 Oct, 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).
Program 1:
For example, program 1 prints 'h' and program 2 prints 'e'.
C++
// 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;
}
Program 2:
C++
// 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;
}
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
OutputJoe's Last Name is Donaldson
Here are some of the programs along with the concept which will help you in understanding the concept better.
When pre/post increment/decrement are used in statements with more than one operand then you have to take care of some rules.
Rule 1. When evaluating pre-increment value should be incremented first and will be assigned at the end of evaluating all the operands.
example :
C++
#include <iostream>
using namespace std;
int main() {
int a=1;
cout<< ++a + a++ ;
}
Now take some time and think of the output of the above program.
If You are thinking that the output should be 4 then you are wrong. The output will be 5.
Here's the explanation
1. pre-increment increments the value first and then assigns it. here ++a is incremented to 2 but is not assigned because a++ is remaining.
2. Then a++ is assigned 2 and then it is incremented to 3. Now the value of a is 3.
3. This final value will be assigned to ++a because pre-increment values are assigned at the last.
4. So the output will be 3+2= 5.
If you think that you understood the concept then try the next example:
What do you think can be the answer to this example?
C++
#include <iostream>
using namespace std;
int main() {
int a=1;
cout<<a++ + ++a;
}
If you think what is the difference between this and the previous example then I must tell you that the difference is in the position of pre-increment and post-increment. And the output will also be changed. You can run this program to check the output.
The output will be 4.
Here's the explanation:
1. First the post-increment will be evaluated. It will be assigned 1 and then incremented to 2.
2. then pre- increment will be incremented to 3 and then assigned to 3.
So the sum of the assigned values is 1+3 = 4
If you like this post then hit the like button and share it with your fellow mates and friends.
- This post has been improved by Triangleofcoding
Similar Reads
Operator Precedence and Associativity in C++
In C++,operator precedence and associativity are important concepts that determine the order in which operators are evaluated in an expression. Operator precedence tells the priority of operators, while associativity determines the order of evaluation when multiple operators of the same precedence l
3 min read
Operator Precedence and Associativity in C
Operator precedence and associativity are rules that decide the order in which parts of an expression are calculated. Precedence tells us which operators should be evaluated first, while associativity determines the direction (left to right or right to left) in which operators with the same preceden
8 min read
Pre-increment and Post-increment in C/C++
In C/C++, Increment operators are used to increase the value of a variable by 1. This operator is represented by the ++ symbol. The increment operator can either increase the value of the variable by 1 before assigning it to the variable or can increase the value of the variable by 1 after assigning
3 min read
match_results prefix() and suffix() in C++
The match_results::prefix() is an inbuilt function in C++ which is used to get the string which is preceding the matched string in the input target string.Syntax: smatch_name.prefix() Note: smatch_name is an object of match_results class.Parameters: This function accept no parameters.Return Value: T
2 min read
Facts and Question related to Style of writing programs in C/C++
Here are some questions related to Style of writing C programs: Question-1: Why i++ executes faster than i + 1 ? Answer-1: The expression i++ requires a single machine instruction such as INR to carry out the increment operation whereas, i + 1 requires more instructions to carry out this operation.
2 min read
Integer literal in C/C++ (Prefixes and Suffixes)
Integer literal is a type of literal for an integer whose value is directly represented in source code. For example, in the assignment statement x = 1, the string 1 is an integer literal indicating the value 1, while in the statement x = 0x10 the string 0x10 is an integer literal indicating the valu
4 min read
Sequence Points in C | Set 1
In this post, we will try to cover many ambiguous questions like following. Guess the output of following programs. // PROGRAM 1 #include <stdio.h> int f1() { printf ("Geeks"); return 1;} int f2() { printf ("forGeeks"); return 1;} int main() { int p = f1() + f2(); return 0;
4 min read
queue push() and pop() in C++ STL
The std::queue::push() and std::queue::pop() functions in C++ STL are used to push the element at the back of the queue and remove the element from the front of the queue respectively. They are the member functions of the std::queue container defined inside the <queue> header file. In this art
2 min read
Left Shift and Right Shift Operators in C/C++
In C/C++, left shift (<<) and right shift (>>) operators are binary bitwise operators that are used to shift the bits either left or right of the first operand by the number of positions specified by the second operand allowing efficient data manipulation. In this article, we will learn
7 min read
Results of comparison operations in C and C++
In C, data type of result of comparison operations is int. For example, see the following program. C/C++ Code #include<stdio.h> int main() { int x = 10, y = 10; printf("%d \n", sizeof(x == y)); printf("%d \n", sizeof(x < y)); return 0; } Output4 4 Whereas in C++, type of
1 min read