Machine-translated by Google. Questions are authored in English.
Top

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;
}

 

  • A.sample friday tuesday sunday
  • B.sample friday tuesday
  • C.sunday tuesday friday sample
  • D.sunday tuesday friday

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.