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

Discussion

What will be the output of the program (myprog.c) given below if it is executed from the command line?
cmd> myprog one two three

/* myprog.c */
#include <stdio.h>
#include <stdlib.h>

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

 

  • A.myprog
  • B.one
  • C.two
  • D.three

Answer: B

Correct answer: one

++argv advances the pointer to argv[1] and yields the new value, so *++argv is argv[1] - the first argument after the program name.

With the command line myprog one two three that is the string "one", and %s prints it in full. The pre-increment is what skips the program name: argv[0] holds that, and shifting the pointer past it is a common idiom when a program wants only its arguments.

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

Post your comment

Please log in or register to post a comment.