Here’s a program that illustrates my problem:
#include <stdio.h>
#define NUMERATOR 8
#define DENOMINATOR 2
#define QUOTIENT (NUMERATOR / DENOMINATOR)
#define ZSTR(x) XSTR(#x)
#define YSTR(x) XSTR(x)
#define XSTR(x) STR(x)
#define STR(x) #x
int main()
{
printf("QUOTIENT: %d\n", QUOTIENT);
printf("STR(QUOTIENT): %s\n", STR(QUOTIENT));
printf("XSTR(QUOTIENT): %s\n", XSTR(QUOTIENT));
printf("YSTR(QUOTIENT): %s\n", YSTR(QUOTIENT));
printf("ZSTR(QUOTIENT): %s\n", ZSTR(QUOTIENT));
return 0;
}
And here’s its output:
$ gcc -g -Wall -o stringify stringify.c && ./stringify
QUOTIENT: 4
STR(QUOTIENT): QUOTIENT
XSTR(QUOTIENT): (8 / 2)
YSTR(QUOTIENT): (8 / 2)
ZSTR(QUOTIENT): "QUOTIENT"
I would like to have a the string literal "4" passed to the compiler, but I’m losing hope.
This is related to this question, but adds a level.
With some tricks you can implement basic arithmetic in a C99 conforming preprocessor. P99 implements arithmetic and logic for small decimal numbers. E.g
would be preprocessed to something like
These macros can be processed further, stringified and everthing you like.
leads to
"XXX_10"as the result of preprocessing.