Precedence of postfix ++ and prefix ++ in C/C++
In C/C++, precedence of Prefix ++ (or Prefix –) has higher 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’.
// Program 1 #include<stdio.h> int main() { char arr[] = "geeksforgeeks"; char *p = arr; ++*p; printf(" %c", *p); getchar(); return 0; } |
chevron_right
filter_none
Output:
h
// Program 2 #include<stdio.h> int main() { char arr[] = "geeksforgeeks"; char *p = arr; *p++; printf(" %c", *p); getchar(); return 0; } |
chevron_right
filter_none
Output:
e
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Operator Precedence and Associativity in C
- match_results prefix() and suffix() in C++
- asctime() and asctime_s() functions in C with Examples
- time.h header file in C with Examples
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
Improved By : sourabh571993



