Friday, February 3, 2023
HomeC ProgrammingC program to seek out sum of two numbers utilizing macro

C program to seek out sum of two numbers utilizing macro


Write a C program so as to add two numbers utilizing macros. Methods to add two numbers utilizing macros #outline preprocessor directive in C program. Logic so as to add two numbers utilizing macros.

In earlier submit we discovered fundamentals about macros. Methods to outline, undefine and redefine a macro in C programming. Right here we are going to proceed from our final lesson. We are going to learn the way we are able to use macros to unravel fundamental programming necessities.

On this submit we are going to study so as to add two numbers utilizing macros.

Required information

Primary C programming, Macros

Methods to add two numbers utilizing macros?

In earlier submit we talked about defining constants utilizing a macro. Nonetheless, you may even remodel a small operate to a macro. Macros execute earlier than compilation of your program therefore are tremendous quick than regular capabilities. Therefore, all the time attempt to convert your small capabilities that doesn’t include any advanced logic to macros.

Allow us to outline a macro that accepts two parameters and return sum of given numbers. 

Syntax:

#outline MACRO_NAME(params) MACRO_BODY

The place MACRO_NAME is title of the macro. params is parameters handed to macro. MACRO_BODY is the physique the place we are going to write precise logic of macro.

Instance:

#outline SUM(x, y) (x + y)

Program so as to add two numbers utilizing macro

/**
 * C program so as to add two numbers utilizing macros
 */

#embrace <stdio.h>

// Outline macro to seek out sum of two numbers
#outline SUM(x, y) (x + y)

int major()
{
    int num1, num2;

    // Enter two numbers from consumer
    printf("Enter any two numbers: ");
    scanf("%dpercentd", &num1, &num2);

    // Calculate and print sum utilizing macro
    printf("Sum(%d, %d) = %dn", num1, num2, SUM(num1, num2));

    return 0;
}
Enter any two numbers: 10 20
Sum(10, 20) = 30

Comfortable coding 😉

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments