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

Pyyhkäise näyttääksesi valikon

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 5

Kysy tekoälyä

expand
ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

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.

Tehtävä

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 5
Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Pahoittelemme, että jotain meni pieleen. Mitä tapahtui?
some-alt