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 1 2 3
cmd> sample 2 2 3
cmd> sample 3 2 3

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

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

 

  • A.sample 3 2 3
  • B.sample 1 2 3
  • C.sample
  • D.Error

Answer: C

Correct answer: sample

argv[0] holds the name by which the program was invoked, and the command line here begins with sample, so that is what is printed - whatever arguments follow.

The three invocations differ only in their arguments, and argv[0] is unaffected by them. Note that the exact contents are a platform convention: Unix-like systems generally give the string as typed, while Turbo C under DOS supplies the full uppercase path, which is what the companion question reflects.

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

Post your comment

Please log in or register to post a comment.