write a c program to adding two number using macro

28 Dec 2022 Balmiki Mandal 0 C Programming

Adding Two Numbers Using a Macro in C

To write a C program to add two numbers using a macro, we can use the following steps:

  1. Define a macro that takes two numbers as input and returns their sum.
  2. Declare two variables to store the two numbers that we want to add.
  3. Use the macro to add the two numbers and store the result in a third variable.
  4. Print the result to the console.

Here is a simple C program to add two numbers using a macro:

C Programming
#include<stdio.h>
#define ADD(x, y) (x + y)
int main() {
  int num1, num2, sum;

  printf("Enter two numbers: ");
  scanf("%d %d", &num1, &num2);

  sum = ADD(num1, num2);

  printf("The sum of %d and %d is %d\n", num1, num2, sum);

  return 0;
}

Output:

Enter two numbers: 10 20
The sum of 10 and 20 is 30

We can also use the macro to add two numbers in an expression. For example, the following code will print the sum of 10 and 20 to the console:

C programming
#define ADD(x, y) (x + y)

int main() {
  printf("The sum of 10 and 20 is %d\n", ADD(10, 20));

  return 0;
}
 Output:
The sum of 10 and 20 is 30

 

Macros can be a useful way to make our code more concise and readable. However, it is important to use them carefully, as they can also make our code more difficult to debug.

Top Resources

write a macro basic program in c

write a program to Multiplication of two number using macros in c

define a macros for finding the biggest of two numbers

write a c program for printing time date using Pre-define Macros

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.