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");
}- A. [LOG] start line 1\n[LOG] end line 1
- B. Compilation error: __LINE__ undefined
- C. Both lines print the same line number
- 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
- What is printed? c #define TRUE 1 #define FALSE 0 #define BOOL int BOOL flag = TRUE; int main(void) { if (flag == TRUE) printf("yes")…
- What is printed? c #define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x)) int main(void) { printf("%d %d %d", CLAMP(5, 1…
- What is printed? Assume int is 32-bit, two's complement. c int x = INTMAX; int y = x + 1; printf("%d", y);
- What is the behavior? c int p = malloc(10 sizeof(int)); free(p); printf("%d", p[0]);
- What is printed? c int i = 1; int arr[5] = {0}; int x = i + i++; printf("%d", x);
- What is the behavior? c int arr[5] = {1,2,3,4,5}; int p = arr + 5; printf("%p", (void)p); printf("%d", p);