Discussion
What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog friday tuesday sunday
/* myprog.c */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%c", *++argv[1]);
return 0;
}
Answer: A
Correct answer: r
Subscripting binds tighter than the prefix ++, so the expression is *(++(argv[1])). It is the pointer stored in argv[1] that is advanced, not argv.
argv[1] points at "friday"; advancing it by one character makes it point at the 'r', and the * yields that character. The contrast with **++argv, which gives 'f', is entirely a matter of which pointer the increment reaches.
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