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.
Please log in or register to report this question.