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

Discussion

What will be the output of the following C statement? (assuming the input is “cool brother in city”)

printf("%s\n", argv[argc]);

 

  • A.(null)
  • B.City
  • C.In
  • D.Segmentation Fault

Answer: A

Correct answer: (null)

argv[argc] is guaranteed to be a null pointer, so the call is printf("%s\n", NULL).

Passing a null pointer to %s is undefined behaviour under the standard, but the GNU C library detects it and prints the literal text (null) rather than dereferencing it - which is the behaviour this key reflects. Other implementations crash instead, so the code should not be written whatever library it happens to meet.

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

Post your comment

Please log in or register to post a comment.