Sum of two numbers using preprocessor directives

Build a simple preprocessor directive for getting the sum of two numbers

Sum of two numbers using a simple macro:

#include <iostream>

#define sum(a, b) a + b

int main()
{
    int a = 3;
    int b = 2;
    std::cout<<sum(a,b);
    return 0;
}

Why use a macro instead of a function?

The first and main reason is portability in different architectures or compilers because all the preprocessor directives are processed before the start of compilation and get’s replaces by corresponding code.

On the second hand a preprocessor directive improves readability of the program making it easy to maintain and can scale verry quickly.

You can read more about C/C++ preprocessors here.

Get The Best Of All Posts Delivered To Your Inbox

Subscribe to our newsletter and stay updated.