Converting Integer Literals to String Literals
There's this preprocessor trick I've learned recently that can convert integer constants to string (char* actually) without the use of any function and it happens before the program is actually compiled.
It involves transforming whatever integer constants you have into strings in order to achieve something like:
puts("This is an integer literal: " INT2STR(2));
// string literal concatenation occurs at compile time. no need for sprintf's, stringstream, or any other run time stuff
This has useful applications including printing out the line number of the offending source:
puts("This code is erroneous at File: " __FILE__ " at Line: " INT2STR(__LINE__) );
The implementation of this macro involves these two lines:
#define INT2STR_(i) #i
#define INT2STR(i) INT2STR_(i)
Note that this is only applicable for string literals and integer literals. Regular integer variables, char* variables and std::string variables would have to undergo the necessary function calls.
It involves transforming whatever integer constants you have into strings in order to achieve something like:
puts("This is an integer literal: " INT2STR(2));
// string literal concatenation occurs at compile time. no need for sprintf's, stringstream, or any other run time stuff
This has useful applications including printing out the line number of the offending source:
puts("This code is erroneous at File: " __FILE__ " at Line: " INT2STR(__LINE__) );
The implementation of this macro involves these two lines:
#define INT2STR_(i) #i
#define INT2STR(i) INT2STR_(i)
Note that this is only applicable for string literals and integer literals. Regular integer variables, char* variables and std::string variables would have to undergo the necessary function calls.
