• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Multi-line C strings and macros

I'm including a GLSL shader in my C program using macros

Code:
#define TO_STRING(A) #A

const char* source = TO_STRING(
void main (void)
{
// do stuff
}	
 );

Which works fine, until I declare several variables at once:

Code:
const char* source = TO_STRING(
void main (void)
{
float a, b, c, d; // this line generates an error
}	
 );

The compiler gives me an error and says I'm passing too many arguments to the TO_STRING() macro. Is there some way around this? Are there a lot of special cases where multi-line strings can't be handled in this way?
 
The multi-linedness isn't the problem. The problem is that you have commas in your argument. That's parsed as:

PHP:
const char* source = TO_STRING("void main (void){float a"," b"," c"," d;}");

You probably don't see this in other cases because commas within parentheses are not counted. I don't see an easy way around this without doing just one declaration per line.
 
Variadiac macros will get you around this I think... This works for me.

Code:
#include <stdio.h>
#define TO_STRING(...) #__VA_ARGS__
int main(int argc, char* argv[])
{
   const char* str = TO_STRING(
                               int main(int argc, char* argv[]){
                               int a,b,c,d;
                               return 0;
                               });


  printf("%s", str);
  return 0;
}
 
Back
Top