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

Discussion

If the different command line arguments are supplied at different times would the output of the following program change?

#include <stdio.h>

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

 

  • A.Yes
  • B.No

Answer: B

Correct answer: No

argv[argc] is guaranteed by the standard to be a null pointer, marking the end of the vector. That is true whatever arguments are supplied, so the value printed never changes.

Note the further problem: the format is %d while the argument is a pointer, which is itself undefined behaviour and typically prints 0. The question turns on the guarantee at the end of the array - a program can therefore walk argv until it meets the null pointer without consulting argc at all.

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

Post your comment

Please log in or register to post a comment.