Saturday, October 1, 2022
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. Find out how 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. Find out how 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 find out how we will use macros to unravel primary programming necessities.

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

Required information

Primary C programming, Macros

Find out how to add two numbers utilizing macros?

In earlier submit we talked about defining constants utilizing a macro. Nevertheless, you’ll be able to even rework a small operate to a macro. Macros execute earlier than compilation of your program therefore are tremendous quick than regular features. Therefore, at all times attempt to convert your small features 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
 */

#embody <stdio.h>

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

int essential()
{
    int num1, num2;

    // Enter two numbers from person
    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

Completely satisfied coding 😉

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments