Built-in Macros Every C Programmer Should Know
These macros are used like regular identifiers β directly in code or within expressions.
They are handled by the preprocessor, before compilation begins.
These require no #define
or #include
. Theyβre built into the language.
main.c
12345678910#include <stdio.h> void process() { printf("Function %s called from %s:%d at %s %s\n", __func__, __FILE__, __LINE__, __DATE__, __TIME__); } int main() { process(); return 0; }
How to use it?
Logging errors with context
This example defines a LOG_ERROR
macro that automatically appends file name and line number to an error message.
main.c
12345678910111213#include <stdio.h> #define LOG_ERROR(msg) \ fprintf(stderr, "ERROR: %s | %s:%d\n", msg, __FILE__, __LINE__) void read_config() { LOG_ERROR("Failed to load config file"); } int main() { read_config(); return 0; }
Function tracing
Automatically prints the function name when it is called β useful for debugging and call tracing.
main.c
123456789101112131415#include <stdio.h> void init_system() { printf("Entering function: %s\n", __func__); } void process_data() { printf("Entering function: %s\n", __func__); } int main() { init_system(); process_data(); return 0; }
Embedding build metadata
Displays the date and time when the program was compiled β helpful for versioning or debugging builds.
main.c
123456#include <stdio.h> int main() { printf("Program compiled on %s at %s\n", __DATE__, __TIME__); return 0; }
Checking compiler standard compliance
The program checks if the compiler conforms to the ISO C standard and prints a corresponding message.
main.c
123456789101112131415#include <stdio.h> int main() { #ifdef __STDC__ printf("Standard-compliant compiler detected.\n"); #ifdef __STDC_VERSION__ printf("C standard version: %ld\n", __STDC_VERSION__); #else printf("Likely using ANSI C (C90)\n"); #endif #else printf("Non-standard compiler.\n"); #endif return 0; }
__STDC_VERSION__
is a numeric macro that indicates the version of the C standard, if supported by the compiler.
Swipe to start coding
You're building a utility that logs important events in the code, including location information (file, line, function), and also prints which C standard the program was compiled with. To do this, you'll use predefined preprocessor macros.
- Fill in the blanks in the
LOG
macro so that it prints the message, file name, line number, and function name. - In
main()
, check if the compiler is standard-compliant using the appropriate macro. - If the C standard version is available, print it.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5.56
Built-in Macros Every C Programmer Should Know
Swipe to show menu
These macros are used like regular identifiers β directly in code or within expressions.
They are handled by the preprocessor, before compilation begins.
These require no #define
or #include
. Theyβre built into the language.
main.c
12345678910#include <stdio.h> void process() { printf("Function %s called from %s:%d at %s %s\n", __func__, __FILE__, __LINE__, __DATE__, __TIME__); } int main() { process(); return 0; }
How to use it?
Logging errors with context
This example defines a LOG_ERROR
macro that automatically appends file name and line number to an error message.
main.c
12345678910111213#include <stdio.h> #define LOG_ERROR(msg) \ fprintf(stderr, "ERROR: %s | %s:%d\n", msg, __FILE__, __LINE__) void read_config() { LOG_ERROR("Failed to load config file"); } int main() { read_config(); return 0; }
Function tracing
Automatically prints the function name when it is called β useful for debugging and call tracing.
main.c
123456789101112131415#include <stdio.h> void init_system() { printf("Entering function: %s\n", __func__); } void process_data() { printf("Entering function: %s\n", __func__); } int main() { init_system(); process_data(); return 0; }
Embedding build metadata
Displays the date and time when the program was compiled β helpful for versioning or debugging builds.
main.c
123456#include <stdio.h> int main() { printf("Program compiled on %s at %s\n", __DATE__, __TIME__); return 0; }
Checking compiler standard compliance
The program checks if the compiler conforms to the ISO C standard and prints a corresponding message.
main.c
123456789101112131415#include <stdio.h> int main() { #ifdef __STDC__ printf("Standard-compliant compiler detected.\n"); #ifdef __STDC_VERSION__ printf("C standard version: %ld\n", __STDC_VERSION__); #else printf("Likely using ANSI C (C90)\n"); #endif #else printf("Non-standard compiler.\n"); #endif return 0; }
__STDC_VERSION__
is a numeric macro that indicates the version of the C standard, if supported by the compiler.
Swipe to start coding
You're building a utility that logs important events in the code, including location information (file, line, function), and also prints which C standard the program was compiled with. To do this, you'll use predefined preprocessor macros.
- Fill in the blanks in the
LOG
macro so that it prints the message, file name, line number, and function name. - In
main()
, check if the compiler is standard-compliant using the appropriate macro. - If the C standard version is available, print it.
Solution
Thanks for your feedback!
Awesome!
Completion rate improved to 5.56single