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>

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

 

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

Answer: C

Correct answer: o

Take the expression from the inside out. ++argv advances the pointer so it points at argv[1], and the pre-increment yields the new value.

The first * then gives argv[1] itself, which is the string "one" - a char *. The second * dereferences that, giving the first character, 'o'. The %c conversion prints it. Note that a single * with %s would have printed the whole word, which is what the companion question does.

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

Post your comment

Please log in or register to post a comment.