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 (turbo c under DOS)?
cmd> sample Good Morning

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

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

 

  • A.3 Good
  • B.2 Good
  • C.Good Morning
  • D.3 Morning

Answer: A

Correct answer: 3 Good

Two things are being asked at once. argc counts the program name along with the arguments, so sample Good Morning gives 3, not 2.

And argv[1] is the first argument after the program name, which is "Good" - argv[0] holds the program name itself. The off-by-one in each is the same idea seen twice: the count and the indexing both include argv[0].

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

Post your comment

Please log in or register to post a comment.