Discussion
What will be the output of the program ?
#include <stdio.h>
int main()
{
char *str;
str = "%d\n";
str++;
str++;
printf(str-2, 300);
return 0;
}
Answer: D
Correct answer: 300
str initially points at the format "%d\n". Two increments advance it two characters, and str-2 then brings it back to where it started.
So printf receives the original format string and prints 300 followed by a newline. The exercise is about pointer arithmetic being exactly reversible: advancing and retreating by the same amount returns the identical address, and the intervening increments leave no trace.
No comment is present. Be the first to comment.
Loading…
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development