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

Discussion

What will be the output of the program if it is executed like below?
cmd> sample

/* sample.c */
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("%s\n", argv[argc-1]);
    return 0;
}

 

  • A.sample
  • B.samp
  • C.No output

Answer: A

Correct answer: sample

argv[0] is the program name as invoked, and with no further arguments argc is 1 - so argv[argc-1] is argv[0], which prints sample. The array always has argc entries plus a null pointer at argv[argc].

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.