Multi-line C strings and macros

Rakehellion

Lifer
Jan 15, 2013
12,181
35
91
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?
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,635
4,562
75
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.
 

randname

Junior Member
May 16, 2011
4
0
0
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;
}