LearnFrenzy
Sign in Take a free test

Variable Number of Arguments · Predict the Output · Question 11

C23 __VA_OPT__ with and without arguments Answer, explanation & discussion

  • Output
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following program, compiled as C23?

main.c
#include <stdio.h>

#define TAG(name, ...) printf("<" name __VA_OPT__(" class='%s'") ">\n" __VA_OPT__(,) __VA_ARGS__)

int main(void) {
    TAG("p");
    TAG("div", "card");
    return 0;
}
  • A <p class=''> <div class='card'>
  • B <p> <div>
  • C <p> <div class='card'> Correct
  • D Compilation error
Correct Answer: Option C

__VA_OPT__(x) expands to x only when variable arguments are present, so the first call gets neither the class text nor the comma.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links