Javatpoint Logo
Javatpoint Logo

Multiline Macros in C

Using the preprocessor directives and macros, reusable code snippets can be defined in C programs. You can construct more intricate code structures that span multiple lines with multiline macros. They are very helpful when writing compound statements or code blocks that will be utilized repeatedly throughout your program. In this article, we will discuss how to create and employ multiline macros in C:

Defining a Multiline Macro:

A multiline macro can be defined by using the #define preprocessor directive. Use the backslash (/) character to indicate that the macro definition continues on the following line after a line break. For defining a multiline macro, use the following basic syntax:

In this example, the MACRO_NAME is the macro's name, which comprises a do-while(0) loop that encloses the multiline code. When the macro is expanded, problems with semicolons are prevented using the do-while(0) loop, making the macro behave like a single statement.

Using a Multiline Macro:

You can utilize a multiline macro like a function call by calling it by its name. Here's an illustration of how the MACRO_NAME macro might be used:

The code defined in the macro is substituted for MACRO_NAME. It is substituted by the preprocessor when it comes across this line.

Program-1:

Output:

Enter a number: 76
76 is Even number

Explanation:

  • The standard input-output library (stdio.h) is included on this line and offers input and output operations functions like printf() and scan() functions.
  • After that, we have defined a multiline macro with the name MACRO. The two inputs for this macro are num, an integer, and str, a string. The specified number and its property-whether odd or even-are printed inside the macro using a series of printf commands predicated on the supplied string.

In the Main() function:

  • The user input is specified as an integer variable named num.
  • The user is asked for a number using the printf() function.
  • An integer is read from the user and stored in the num variable using the scanf
  • The program returns an exit status of 0, indicating successful execution, and the main function exits.

Complexity Analysis:

Time Complexity:

The printf and scanf functions have a time complexity inversely correlated with the number of characters printed or read. Still, we frequently treat them as constant time, O(1), because the number of characters is frequently small and unrelated to the input size.

Conditional Check: The if (num & 1) conditional check uses a bitwise AND operation with an O(1) constant time complexity.

Printing: The complexity of this function is constant time, or O(1), because there are a given number of printf instructions inside the if and else branches, and the number is independent of input size.

The code's overall time complexity is O(1) because it is independent of input size.

Space Complexity:

Variables: It has constant space complexity (O(1)) because num is a single integer variable.

Temporary Space: The scanf function needs minimal amounts of temporary space (which can be regarded as constant, O(1)) for input buffering.

Function call: The overhead is required by the printf and scanf function calls. However, this cost is little and can be regarded as constant, O(1).

Constant space complexity (O(1)) characterizes the code. The 'num' and other variables occupy a fixed amount of space. The 'scanf's temporary space is minimal. The overhead of the function calls ('printf' and 'scanf') is quite low. Minimal memory variance is introduced through conditional execution ('if'). Macro expansion needs limited space, which is then released after expansion. As a result, input size has no impact on memory use.

Program-2:

Output:

Before swapping: x = 5, y = 10
After swapping: x = 10, y = 5

Explanation:

  • Here, we're creating the SWAP macro, which accepts the arguments a and b. This macro's goal is to change the values of two variables.
  • After that, the do-while(0) loop opens the macro. The macro can be used everywhere, and a single statement is anticipated by acting like a single statement with the help of the loop. The while(0) and do commands are used to execute the code.
  • A temporary variable named temp is set up inside the loop and given the value of a. During the swapping operation, the value of a is temporarily stored in this temporary variable.
  • After that, the value of b is given to the value of a.
  • To effectively swap the values of a and b, the value stored in the temp variable is assigned to the value of b.

In the Main function:

  • The initial values of the two integer variables x and y are 5 and 10, respectively.
  • The x and y values before the swapping operation are printed using the printf function.
  • Invoking the SWAP macro with the inputs x and y effectively swaps their values.
  • After the swapping procedure, the printf function is again used to print the x and y values.

The macro SWAP makes the swapping process easier by offering an easy way to swap the values of two variables without having to type out the temporary variable and assignments each time explicitly.

Complexity Analysis:

Time Complexity:

  • Int x = 5 and Int y = 10 are declared and initialized with constant time complexity, O(1).
  • Printing: The O(1) constant time complexity of the printf function's request to print the x and y values before and after switching is used.
  • Swapping: The SWAP macro performs three straightforward assignments requiring constant time, or O(1).
  • As a result, the code's overall time complexity is O(1), which means that the execution time is independent of the input size.

Space Complexity:

  • The space complexity measures the program's memory usage. In the sample of code provided:
  • Declared variables x and y take up O(1) constant space complexity.
  • Temporary Variable: O(1) constant space is consumed by the temp variable utilized in the swapping operation.
  • Function Calls: The printf function calls take a tiny bit of memory for function call overhead, but the quantity is insignificant and can be regarded as constant, O(1).
  • As a result, the code has a space complexity of O(1), meaning it does not scale with the input size.

Benefits of Multiline Macro:

There are several benefits of multiline macros in C. Some main benefits of multiline macros in C are as follows:

Modularity and Reusability: By condensing complex processes or coding patterns into a single macro, multiline macros encourage modularity and reusability among various components of your program.

Code reduction: By offering a simple means to describe repetitive or boilerplate code, macros can help decrease code duplication, resulting in a more compact and maintainable codebase.

Consistency: By describing complicated or specialized actions in macros, you may guarantee consistency in behaviour across your entire program, lowering the possibility of errors brought on by inconsistent implementation.

Conditional compilation: You can keep a single codebase for several contexts using macros with conditional logic to toggle features or create platform-specific code.

Challenges of Multiline Macro:

There are several challenges of multiline macros in C. Some main challenges of multiline macros in C are as follows:

Limited Expressivity: Although multiline macros may handle complex code blocks, they are less expressive than functions because they need local variables and appropriate scoping.

Maintainability: Excessive use of multiline macros may result in difficult-to-understand code, making maintenance and debugging more challenging.

Compilation Time: As complicated macros must be expanded and generated again, using them frequently might increase compilation times.

Code Bloating: Depending on how macros are utilized, if they are enlarged several times throughout the program, it could impact the executable size and performance.


Next TopicScanset in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA