Hi
I have a macro to do concatenation as below
#define dd Cloud
#define ff _Storage
#define CCM_BRAND_CONCAT2(a,b) a##b
The output is "Cloud _Storage" . Somehow theres extra space that gets added. Is there a way to remove the unawanted characters
Pre-processor macros are very limited in what they can do.
And, generally, you don't need a special macro to "concatenate" string literals. You can simply do:
1 2 3
|
#define FOO "foo"
#define BAR "bar"
const char* mystr = FOO BAR;
| |
...which will be expanded to:
const char* mystr = "foo" "bar"; // equivalent to: const char* mystr = "foobar";
If you actually want to concatenate arbitrary strings
at runtime, then you should be using:
https://cplusplus.com/reference/cstring/strcat/
https://cplusplus.com/reference/cstring/strncat/
...or, even better, work with
std::string
objects right away! ;-)
________
Anyhow, if you have a macro like the following...
#define MY_CONCAT(a,b) a##b
...then
MY_CONCAT(foo,bar)
will be expanded to
exactly foobar
. That is
not a string though!
________
Strings can be created via macros using the following syntax:
#define MAKE_STR(s) #s
With the above macro definition,
MAKE_STR(foo)
will be expanded to
"foo"
.
Last edited on