No, as James said, there is no way to split tokens in the preprocessor or find out the length of a string.
But I think that for your use case this is not necessary at all. The line that you would fail with your argument with #x is a constant-size string, for example, chicken leads to "chicken" , which simply has type char[8] . The length of such a line is a compile-time constant, and you can simply detect it with sizeof :
#define TOKLEN(TOK) (sizeof(#TOK)-1)
Using this kind of thing in C would just look
#define SCARY(TOK) for (size_t i = 0; i < TOKLEN(TOK); ++i) printf("%c:",
Since TOKLEN(TOK) is a compile-time constant, the compiler can expand it if necessary.
To use this in my use case for C ++
template < size_t n > class constLenString { size_t const len = n; char const* str; constLenString(char* s) : str(s) { } }; #define defConstLenString(TOK, NAME) constLenString< TOKLEN(TOK) > NAME(#TOK)
(untested, my C ++ is rusty)
and now with
defConstLenString(chicken, chick);
chick.n is a constant that can be the boundary of a for loop or any other, and the compiler should be able to fully optimize everything.
source share