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

Stryg for at vise menuen

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.

main.c

main.c

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.

main.c

main.c

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.

main.c

main.c

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.

main.c

main.c

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.

main.c

main.c

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.

Opgave

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øsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 5
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

close

Awesome!

Completion rate improved to 5.56

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.

main.c

main.c

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.

main.c

main.c

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.

main.c

main.c

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.

main.c

main.c

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.

main.c

main.c

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.

Opgave

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øsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

close

Awesome!

Completion rate improved to 5.56

Stryg for at vise menuen

some-alt