__CONCAT(3)

πŸ“¦ version 11.0, in nixpkgs unstable

πŸ’… __CONCAT(3) β€” okay bestie, let's talk about the __CONCAT and __STRING macros

So this is a page from the Library Functions Manual (the 3 in __CONCAT(3) is giving "section 3, library functions" ✨). Sit down, grab a strawberry πŸ“, and let's get into it.


🌸 NAME 🌸

__CONCAT, __STRING β€” argument substitution, but make it preprocessor chic πŸŽ€


✨ SYNOPSIS ✨

#include <sys/cdefs.h>

xy
__CONCAT(x, y);

const char *
__STRING(x);

(Yes, xy up there is literally the return-type placeholder from the page β€” I'm not touching it, she stays exactly as written πŸ’…)


πŸ’– DESCRIPTION πŸ’–

Okay so first up, the __CONCAT macro. It's lowkey just vibing with the cpp(1) preprocessor to glue two tokens together. When the macro expands, x and y get combined into a single token β€” but only if the result actually forms a valid token. If the two tokens together don't make a valid token, then nope, they can't be concatenated, periodt. This whole thing is known as "token concatenation" or "token pasting". 🫢

Then there's __STRING(), the icon. It uses the conventional '#' preprocessing operator to swap the argument x out for a string literal. She's also known as "stringification". Slay. 🌈


πŸ¦‹ EXAMPLES πŸ¦‹

Not me showing you that these two printf(3) calls produce the same output πŸ‘€ :

#define Net     0x01
#define BSD     0x02

#define NetBSD  "NetBSD"

(void)printf("%s\n", __CONCAT(Net, BSD));
(void)printf("%s%s\n", __STRING(Net), __STRING(BSD));

Same output, two different vibes. Main character energy from both fr fr. πŸ’œ


πŸŽ€ SEE ALSO πŸŽ€

Go read them, iykyk πŸ“š


πŸ‘‘ HISTORY πŸ‘‘

The __CONCAT() and __STRING() macros first appeared in NetBSD 1.3. Little baby macros, all grown up now πŸ˜­πŸ’–


πŸ”₯ CAVEATS πŸ”₯

Bestie, listen to me carefully, because there are many small details that direct the proper use of these macros and I need you to focus πŸ™ˆ:

  • While all leading and trailing whitespace is ignored when __STRING() is used, it is undefined whether cpp(1) puts white space between the tokens when __CONCAT() is used. Undefined! Do not come crying to me about whitespace. πŸ’€
  • Also worth noting: the C preprocessor converts all comments to whitespace before any macros are even considered.
  • The use of either macro is discouraged in complex constructs. Keep it simple, keep it cute. 🧚
Use of this macro is non-portable β€” she is part of the implementation namespace and should only be used in NetBSD code. No sneaking her into your portable project, ngl. 🚨

⭐ πŸ’… ⭐

And that's the page! Source/version and date, for the record: this one's from Debian, dated October 17, 2013 β€” so yes, she's an elder icon but she still ate. Byeee πŸ«ΆπŸ’–πŸŽ€

πŸ“¦ where this page comes from