MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Common Predefined Macros

In C, the preprocessor defines several predefined macros that provide information about the environment and compilation settings. These predefined names are automatically set by the compiler and can be used to write portable and conditional code.

Common Predefined Macros:

  1. __FILE__: Expands to the name of the current source file as a string literal. It helps in identifying the source file where an error or log message originated.
  2. __LINE__: Expands to the current line number in the source file. This is useful for debugging and error reporting.
  3. __DATE__: Expands to a string literal representing the date when the source file was compiled, in the format "Mmm dd yyyy".
  4. __TIME__: Expands to a string literal representing the time when the source file was compiled, in the format "hh:mm
  5. __STDC__: Defined as 1 if the compiler adheres to the ANSI C standard. This macro can be used to check for standard compliance.
  6. __cplusplus: Defined if the compiler is a C++ compiler, and expands to a version number representing the C++ standard supported by the compiler.
  7. __GNUC__: Defined by GCC (GNU Compiler Collection), this macro expands to the version number of the GCC compiler. It helps in detecting the version of GCC being used.
  8. _WIN32: Defined when compiling for a 32-bit Windows environment. It is used to include or exclude Windows-specific code.
  9. _WIN64: Defined when compiling for a 64-bit Windows environment. It allows for the inclusion or exclusion of code specific to 64-bit Windows.
  10. __linux__: Defined when compiling for a Linux environment. It helps in writing platform-specific code for Linux systems.
  11. __APPLE__: Defined when compiling for macOS or iOS systems. It can be used to include or exclude code specific to Apple's platforms.
  12. __unix__: Defined when compiling for a Unix-like system. It helps in managing code that should be compiled on Unix-based systems.

These predefined macros allow programmers to write code that can adapt to different environments and compiler settings, making it easier to develop portable and maintainable software.

Report an issue

Reporting: Common Predefined Macros (topic)

Related Posts