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 sizeofargv, char *argv[])
{
while(sizeofargv)
printf("%s", argv[--sizeofargv]);
return 0;
}
Answer: C
Correct answer: sunday tuesday friday sample
The parameter is merely named sizeofargv; it is still the argument count, which is 4 here. The loop prints argv[--sizeofargv], so the indices run 3, 2, 1, 0.
That gives "sunday", "tuesday", "friday" and finally "sample" - the arguments in reverse order with the program name last, since argv[0] is included. There is no separator in the format string, so the words in fact run together in the output.
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