C Macros

C macros are a feature of the C programming language that allow you to define reusable code snippets. Macros are preprocessor directives that are processed before the compilation of the code. They are primarily used for code generation and simple text substitution.

Here’s the general syntax of a C macro:

#define MACRO_NAME(parameter_list) replacement_text

The #define directive is used to define a macro, followed by the macro name, a parameter list (optional), and the replacement text.

Macros can be classified into two types: object-like macros and function-like macros.

  1. Object-like Macros: An object-like macro is defined without any parameters and behaves like a constant text substitution. Whenever the macro name appears in the code, it is replaced with the specified text.

    Example:

#define PI 3.14159
float area = PI * radius * radius;

In the above example, every occurrence of PI in the code will be replaced with 3.14159. It simplifies the code and avoids repetitive typing.

2. Function-like Macros: A function-like macro is defined with a parameter list and behaves like a function call. When the macro is used in the code, the parameters are substituted in the replacement text.

Example:

#define MAX(a, b) ((a) > (b) ? (a) : (b))
int larger = MAX(x, y);
  1. In this example, the macro MAX takes two parameters (a and b) and returns the maximum of the two. The macro is expanded as ((x) > (y) ? (x) : (y)), which is then evaluated.

Macros can also include conditional statements, loops, and other preprocessor directives to generate more complex code. However, it’s important to use macros judiciously as they can sometimes lead to unexpected behavior and make the code harder to understand and debug.

To improve code readability and maintainability, it’s generally recommended to use inline functions or constants instead of macros whenever possible. Inline functions provide similar benefits to macros but offer better type checking and error handling.

C Predefined Macros:

C programming language provides several predefined macros that are automatically defined by the compiler. These macros give information about the compilation environment, such as the compiler version, platform, and compilation options. Predefined macros are often used to write portable code that can adapt to different environments.

Here are some commonly used predefined macros in C:

  1. __FILE__: This macro expands to the current source file name as a string literal.
  2. __LINE__: This macro expands to the current line number as an integer.
  3. __DATE__: This macro expands to a string literal that represents the date of compilation in “MMM DD YYYY” format. For example, “May 12 2023”.
  4. __TIME__: This macro expands to a string literal that represents the time of compilation in “HH:MM:SS” format. For example, “15:30:45”.
  5. __STDC__: This macro is defined to indicate standard C compliance. Its value is typically a non-zero integer.
  6. __STDC_VERSION__: This macro expands to a long integer representing the version of the C standard being used. For example, for C99, the value is 199901L.
  7. __cplusplus: This macro is defined by C++ compilers and indicates that the code is being compiled as C++ rather than C.
  8. __GNUC__, __clang__, __ICC, etc.: These macros are specific to certain compilers and are defined with a non-zero value if the code is being compiled with that particular compiler.

These are just a few examples of the predefined macros available in C. The specific macros and their values can vary depending on the compiler and platform being used. You can refer to the compiler’s documentation for a comprehensive list of predefined macros specific to your compiler.

C predefined macros example:

Certainly! Here’s an example that demonstrates the usage of some predefined macros:

#include <stdio.h>

int main() {
    printf("File: %s\n", __FILE__);
    printf("Line: %d\n", __LINE__);
    printf("Date: %s\n", __DATE__);
    printf("Time: %s\n", __TIME__);

#ifdef __STDC__
    printf("Standard C: Yes\n");
#else
    printf("Standard C: No\n");
#endif

#ifdef __STDC_VERSION__
    printf("C Standard Version: %ld\n", __STDC_VERSION__);
#endif

#ifdef __cplusplus
    printf("Compiled as C++\n");
#else
    printf("Compiled as C\n");
#endif

#ifdef __GNUC__
    printf("Compiled with GCC\n");
#endif

#ifdef __clang__
    printf("Compiled with Clang\n");
#endif

#ifdef __ICC
    printf("Compiled with Intel Compiler\n");
#endif

    return 0;
}

When you compile and run this program, you’ll get output similar to the following:

File: example.c
Line: 6
Date: May 12 2023
Time: 15:30:45
Standard C: Yes
C Standard Version: 201112L
Compiled as C
Compiled with GCC

The output may vary depending on the compiler and platform you are using. The example demonstrates the usage of macros like __FILE__, __LINE__, __DATE__, __TIME__, __STDC__, __STDC_VERSION__, __cplusplus, and compiler-specific macros like __GNUC__, __clang__, and __ICC__.