Bird
Raised Fist0
Cprogramming~20 mins

Predefined macros - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Predefined Macros Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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;
}
AFile: main.c, Line: 7
BFile: main.c, Line: 6
CFile: main.c, Line: 4
DFile: main.c, Line: 5
Attempts:
2 left
💡 Hint
Remember that __LINE__ expands to the line number where it appears in the source code.
❓ Predict Output
intermediate
2: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;
}
ACompiled on Jun 15 2024 at 10:00:00 (example)
BCompiled on 2024-06-15 at 10:00:00
CCompiled on Jun 15 2024 at 10:00:00
DCompiled on Jun 15 2024 at 10:00:00 (actual compile time)
Attempts:
2 left
💡 Hint
The __DATE__ macro expands to a string literal in the format "Mmm dd yyyy".
❓ Predict Output
advanced
2: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;
}
AStandard C compiler detected
BNon-standard compiler
CCompilation error due to undefined macro
DNo output
Attempts:
2 left
💡 Hint
The __STDC__ macro is defined by standard C compilers.
❓ Predict Output
advanced
2: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;
}
AFunction name: main
BFunction name: greet
CFunction name: __func__
DFunction name: (null)
Attempts:
2 left
💡 Hint
The __func__ macro expands to the name of the current function as a string.
🧠 Conceptual
expert
2: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;
}
ALine: 9
BLine: 5
CLine: 8
DLine: 7
Attempts:
2 left
💡 Hint
The __LINE__ macro expands to the line number where it appears after macro expansion.

Practice

(1/5)
1. Which of the following predefined macros in C gives the current line number in the source code?
easy
A. __LINE__
B. __FILE__
C. __DATE__
D. __TIME__

Solution

  1. Step 1: Understand predefined macros purpose

    Predefined macros provide info about the code or environment automatically.
  2. Step 2: Identify macro for line number

    __LINE__ gives the current line number in the source file.
  3. Final Answer:

    __LINE__ -> Option A
  4. Quick Check:

    Line number macro = __LINE__ [OK]
Hint: Line number macro always has LINE in its name [OK]
Common Mistakes:
  • Confusing __FILE__ with line number
  • Using __DATE__ or __TIME__ for line info
  • Thinking __LINE__ is a function
2. Which of the following is the correct syntax to print the current file name using a predefined macro in C?
easy
A. printf("File: %s", FILE);
B. printf("File: %s", __FILE__);
C. printf("File: %s", _FILE_);
D. printf("File: %s", file);

Solution

  1. Step 1: Recall predefined macro for file name

    The macro __FILE__ holds the current source file name as a string.
  2. Step 2: Check correct syntax usage

    Using printf("File: %s", __FILE__); correctly prints the file name.
  3. Final Answer:

    printf("File: %s", __FILE__); -> Option B
  4. Quick Check:

    File name macro syntax = __FILE__ [OK]
Hint: Predefined macros always have double underscores before and after [OK]
Common Mistakes:
  • Omitting underscores in __FILE__
  • Using FILE without underscores
  • Using lowercase file instead of macro
3. What will be the output of the following C code snippet?
#include <stdio.h>
int main() {
    printf("Date: %s\n", __DATE__);
    printf("Time: %s\n", __TIME__);
    return 0;
}
medium
A. Compilation error due to undefined macros
B. Date: __DATE__\nTime: __TIME__
C. Date: current date at compilation\nTime: current time at compilation
D. Date: 0000-00-00\nTime: 00:00:00

Solution

  1. Step 1: Understand __DATE__ and __TIME__ macros

    These macros expand to strings representing the date and time when the code was compiled.
  2. Step 2: Predict output of printf statements

    The program prints the compilation date and time as strings, not the macro names or errors.
  3. Final Answer:

    Date: current date at compilation\nTime: current time at compilation -> Option C
  4. Quick Check:

    __DATE__ and __TIME__ show compile date/time [OK]
Hint: __DATE__ and __TIME__ show compile time info as strings [OK]
Common Mistakes:
  • Expecting runtime current date/time
  • Thinking macros print their own names
  • Assuming macros are undefined
4. Consider this C code snippet:
#include <stdio.h>
int main() {
    printf("Line: %d\n", LINE);
    return 0;
}

What is the problem with this code?
medium
A. LINE is not a predefined macro; it should be __LINE__
B. Missing semicolon after printf
C. printf format specifier %d is incorrect for LINE
D. No problem; code runs fine

Solution

  1. Step 1: Identify the macro usage error

    The code uses LINE instead of the correct predefined macro __LINE__.
  2. Step 2: Understand consequences

    Since LINE is undefined, this causes a compilation error.
  3. Final Answer:

    LINE is not a predefined macro; it should be __LINE__ -> Option A
  4. Quick Check:

    Predefined macros need double underscores [OK]
Hint: Predefined macros always have double underscores before and after [OK]
Common Mistakes:
  • Using LINE without underscores
  • Thinking %d is wrong for line number
  • Ignoring missing macro error
5. You want to log the current file name, line number, and compilation date in your C program for debugging. Which code snippet correctly uses predefined macros to do this?
hard
A. printf("File: %s, Line: %d, Date: %s\n", _FILE_, _LINE_, _DATE_);
B. printf("File: %s, Line: %d, Date: %s\n", FILE, LINE, DATE);
C. printf("File: %s, Line: %d, Date: %s\n", "__FILE__", "__LINE__", "__DATE__");
D. printf("File: %s, Line: %d, Date: %s\n", __FILE__, __LINE__, __DATE__);

Solution

  1. Step 1: Identify correct predefined macros

    The macros __FILE__, __LINE__, and __DATE__ provide file name, line number, and compile date respectively.
  2. Step 2: Check correct usage in printf

    Using them directly without quotes and with correct underscores prints the desired info.
  3. Final Answer:

    printf("File: %s, Line: %d, Date: %s\n", __FILE__, __LINE__, __DATE__); -> Option D
  4. Quick Check:

    Use double underscore macros directly in printf [OK]
Hint: Use __FILE__, __LINE__, __DATE__ directly without quotes [OK]
Common Mistakes:
  • Using macros without underscores
  • Putting macros inside quotes (prints names, not values)
  • Using single underscores instead of double
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard