What are the implications of using static const instead of #define?

Posted by Simon Elliott on Stack Overflow See other posts from Stack Overflow or by Simon Elliott
Published on 2009-09-25T11:45:09Z Indexed on 2012/04/05 17:31 UTC
Read the original article Hit count: 196

Filed under:

gcc complains about this:

#include <stdio.h>
static const int YY = 1024;
extern int main(int argc, char*argv[])
{  
  static char x[YY];
}

$ gcc -c test1.c test1.c: In function main': test1.c:5: error: storage size of x' isn't constant test1.c:5: error: size of variable `x' is too large

Remove the “static” from the definition of x and all is well.

I'm not exactly clear what's going on here: surely YY is constant?

I had always assumed that the "static const" approach was preferable to "#define". Is there any way of using "static const" in this situation?

© Stack Overflow or respective owner

Related posts about c