Predict the output of following C programs.
The output of above programs and all such programs can be easily guessed by remembering following simple rules about postfix ++, prefix ++ and * (dereference) operators
1) Precedence of prefix ++ and * is same. Associativity of both is right to left.
2) Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.
(Refer: Precedence Table)
The expression ++*p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). Therefore the output of first program is “arr = 11, arr = 20, *p = 11“.
The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. Therefore the output of second program is “arr = 10, arr = 20, *p = 20“.
The expression *++p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p). Therefore the output of third program is “arr = 10, arr = 20, *p = 20“.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Difference Between GIT and SVN
- Difference between CLI and GUI
- Difference between LAN and MAN
- Difference between ELT and ETL
- Difference between DML and TCL
- Difference between RPC and RMI
- Difference between H.323 and SIP
- Difference Between Gi-Fi and Li-Fi
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- Difference between LAN and WAN
- Difference Between SMO and SEO
- Difference between IoT and M2M
- Difference between LAN, MAN and WAN
- Difference between DFD and ERD
- What is the difference between GUI and CUI?
- Difference between CD and DVD
- Difference between DFA and NFA
- Difference between PNG and GIF
- Difference between T-SQL and PL-SQL
- Difference between BFS and DFS
Improved By : srinam