Challenge - 5 Problems
Predefined Macros Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of __FILE__ and __LINE__ macros
What is the output of this C program when compiled and run?
C
#include <stdio.h> int main() { printf("File: %s, Line: %d\n", __FILE__, __LINE__); return 0; }
Attempts:
2 left
💡 Hint
Remember that __LINE__ expands to the line number where it appears in the source code.
✗ Incorrect
The __FILE__ macro expands to the current file name as a string. The __LINE__ macro expands to the line number where it is used. The printf statement is on line 5 in the code snippet, so it prints Line: 5.
❓ Predict Output
intermediate2:00remaining
Output of __DATE__ and __TIME__ macros
What will this program print when compiled and run?
C
#include <stdio.h> int main() { printf("Compiled on %s at %s\n", __DATE__, __TIME__); return 0; }
Attempts:
2 left
💡 Hint
The __DATE__ macro expands to a string literal in the format "Mmm dd yyyy".
✗ Incorrect
The __DATE__ macro expands to the date when the source file was compiled, in the format "Mmm dd yyyy" (e.g., "Jun 15 2024"). The __TIME__ macro expands to the time of compilation in "hh:mm:ss" format. The output matches option C.
❓ Predict Output
advanced2:00remaining
Output of __STDC__ macro
What is the output of this program if compiled with a standard C compiler?
C
#include <stdio.h> int main() { #ifdef __STDC__ printf("Standard C compiler detected\n"); #else printf("Non-standard compiler\n"); #endif return 0; }
Attempts:
2 left
💡 Hint
The __STDC__ macro is defined by standard C compilers.
✗ Incorrect
The __STDC__ macro is predefined by standard C compilers to indicate compliance with the ANSI C standard. The #ifdef check succeeds, so the program prints "Standard C compiler detected".
❓ Predict Output
advanced2:00remaining
Output of __func__ macro inside a function
What does this program print when run?
C
#include <stdio.h> void greet() { printf("Function name: %s\n", __func__); } int main() { greet(); return 0; }
Attempts:
2 left
💡 Hint
The __func__ macro expands to the name of the current function as a string.
✗ Incorrect
The __func__ macro expands to the name of the function where it is used. Since it is inside the greet() function, it prints "greet".
🧠 Conceptual
expert2:00remaining
Behavior of __LINE__ macro with macro expansion
Consider the following code. What is the output when compiled and run?
C
#include <stdio.h> #define PRINT_LINE() printf("Line: %d\n", __LINE__) int main() { PRINT_LINE(); return 0; }
Attempts:
2 left
💡 Hint
The __LINE__ macro expands to the line number where it appears after macro expansion.
✗ Incorrect
The __LINE__ macro expands to the line number in the source code where it is used after macro expansion. Since PRINT_LINE() is called on line 9, __LINE__ expands to 9, so the output is "Line: 9".