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 friday tuesday sunday

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

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

 

  • A.s
  • B.f
  • C.sample
  • D.friday

Answer: B

Correct answer: f

++argv advances the pointer to argv[1], so the first * gives the string "friday" and the second gives its first character, 'f'.

Compare with *++argv[1], which increments the pointer inside the array rather than the array pointer itself and yields 'r'. Which pointer moves is decided by precedence: subscripting binds tighter than the prefix ++, so ++argv[1] increments argv[1], while ++argv increments argv.

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

Post your comment

Please log in or register to post a comment.