C/C++ Macro for string concatenation

CC++Server Side ProgrammingProgramming

In this program we will see how the macros are used to concatenate two strings. We can create two or more than two strings in macro, then simply write them one after another to convert them into a concatenated string. The syntax is like below:

#define STR1 "str1"
#define STR2 " str2"
#define STR3 STR1 STR2 //it will concatenate str1 and str2

Input: Take two strings

Output: Return concatenated string.

Algorithm

Step 1:Take two strings
Step 2: Use macro to concatenate the strings
Step 3: End

Example Code

 Live Demo

#include<stdio.h>
#define STR1 "Hello"
#define STR2 "World"
#define STR3 STR1 STR2
main() {
   printf("%s", STR3);
}

Output:

HelloWorld
raja
Published on 28-Mar-2019 08:03:57
Advertisements