📖 Scenario: You are writing a simple C program that needs to display information about when and where it was compiled. This is useful for debugging and tracking versions.
🎯 Goal: Build a C program that uses predefined macros to show the current date, time, and file name of the source code.
📋 What You'll Learn
Use the predefined macro __DATE__ to get the compilation date
Use the predefined macro __TIME__ to get the compilation time
Use the predefined macro __FILE__ to get the source file name
Print these values clearly labeled
💡 Why This Matters
🌍 Real World
Developers often use predefined macros to embed build information in programs for debugging and version tracking.
💼 Career
Knowing how to use predefined macros is useful for software developers working on C projects, especially in embedded systems and system programming.
Progress0 / 4 steps
1
Create a C program skeleton
Write the initial C program with #include <stdio.h> and an empty main function that returns 0.
C
Hint
Start by including the standard input-output header and create the main function.
2
Add variables for predefined macros
Inside main, create three const char* variables named compile_date, compile_time, and source_file. Assign them the values __DATE__, __TIME__, and __FILE__ respectively.
C
Hint
Use the predefined macros exactly as shown to assign the variables.
3
Print the predefined macro values
Use printf to display the labels and values of compile_date, compile_time, and source_file on separate lines.
C
Hint
Use printf with %s to print each string variable with a label.
4
Run and display the output
Run the program and print the output showing the compilation date, time, and source file name.
C
Hint
Run the program to see the compilation details printed.
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
Step 1: Understand predefined macros purpose
Predefined macros provide info about the code or environment automatically.
Step 2: Identify macro for line number
__LINE__ gives the current line number in the source file.
Final Answer:
__LINE__ -> Option A
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
Step 1: Recall predefined macro for file name
The macro __FILE__ holds the current source file name as a string.
Step 2: Check correct syntax usage
Using printf("File: %s", __FILE__); correctly prints the file name.
Final Answer:
printf("File: %s", __FILE__); -> Option B
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?
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
Step 1: Identify the macro usage error
The code uses LINE instead of the correct predefined macro __LINE__.
Step 2: Understand consequences
Since LINE is undefined, this causes a compilation error.
Final Answer:
LINE is not a predefined macro; it should be __LINE__ -> Option A
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
Step 1: Identify correct predefined macros
The macros __FILE__, __LINE__, and __DATE__ provide file name, line number, and compile date respectively.
Step 2: Check correct usage in printf
Using them directly without quotes and with correct underscores prints the desired info.