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

Desliza para mostrar el menú

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.

Tarea

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 5
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

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.

Tarea

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

close

Awesome!

Completion rate improved to 5.56

Desliza para mostrar el menú

some-alt