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[2] );
    return 0;
}

 

  • A.s
  • B.f
  • C.u
  • D.r

Answer: C

Correct answer: u

Subscripting binds tighter than the prefix ++, so this is *(++(argv[2])): the pointer held in argv[2] is advanced by one character.

argv[2] is "tuesday", so advancing past the 't' leaves it pointing at the 'u', which the * yields. The pattern is worth learning as a family: **++argv gives the first letter of the first argument, while *++argv[n] gives the second letter of argument n.

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

Post your comment

Please log in or register to post a comment.