Disclaimer: I am not a C programmer.
I have recently seen a friend's project. Due to reasons I don't understand, he writes code in a string which is compiled at runtime. This results in something like:
char x[] = "int y = 5; printf(\"%i\", y)";
run_this_code(x);
Which is horrible to use because Visual Studio doesn't step in and do syntax highlighting etc.
Using some preprocessor abuse, it is possible to do trick Visual Studio into thinking you're writing real code and then having the preprocessor turn it into a string before the compiler gets hold of your source. This works:
#define STRINGIFY(x) #x
int main(void){
char[] y = STRINGIFY(
int x = 5;
printf("%i", x);
);
printf("%s", y);
}
The problem with this is it prints out:
int x = 5; printf("%i\n", x);
The problem then is the runtime compiler says Error on line 1. Is there a way of making it include the newlines?
Update This is not my problem. It is someone else's code, I just became interested in the idea of using the preprocessor to make his life easier. I have no idea why he's doing it like this.
Update removed all mention of CUDA because this question is about the preprocessor, not CUDA.