Discussion
What will be the output of the program (sample.c) given below if it is executed from the command line?
cmd> sample friday tuesday sunday
/* sample.c */
#include <stdio.h>
int main(int argc, char *argv[])
{
printf("%c", *++argv[2] );
return 0;
}
Answer: C
Correct answer: u
Subscripting binds tighter than the prefix ++, so this is *(++(argv[2])): the pointer held in argv[2] is advanced by one character.
argv[2] is "tuesday", so advancing past the 't' leaves it pointing at the 'u', which the * yields. The pattern is worth learning as a family: **++argv gives the first letter of the first argument, while *++argv[n] gives the second letter of argument n.
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