ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
#define LOG(msg) printf("[LOG] %s line %d\n", msg, __LINE__)
int main(void) {
    LOG("start");
    LOG("end");
}
  1. A. [LOG] start line 1\n[LOG] end line 1
  2. B. Compilation error: __LINE__ undefined
  3. C. Both lines print the same line number
  4. D. [LOG] start line 2\n[LOG] end line 3 (with the actual line numbers of each call site)

Correct answer: D. [LOG] start line 2\n[LOG] end line 3 (with the actual line numbers of each call site)

__LINE__ is a predefined macro that expands to the current source line number at each point of use, so each LOG invocation captures its own distinct call-site line number, not a fixed or shared value.

More C Programming questions

Exams that ask this topic