Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Built-in Macros Every C Programmer Should Know | Compiler Directives and Advanced Control
C Preprocessing

Swipe um das Menü anzuzeigen

book
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.

c

main

copy
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.

c

main

copy
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.

c

main

copy
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.

c

main

copy
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.

c

main

copy
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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 5
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

book
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.

c

main

copy
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.

c

main

copy
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.

c

main

copy
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.

c

main

copy
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.

c

main

copy
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.

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 5
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?
some-alt