6
static struct K {  int x; };

Is this valid in C and C++?

Bala R
  • 107,317
  • 23
  • 199
  • 210
Henry_1089
  • 63
  • 3
  • 3
    Why not just try it and see? When you find out that it _isn't_ ask what your error is. – San Jacinto Mar 05 '11 at 17:47
  • 4
    You have no access to a compiler or something? – Darin Dimitrov Mar 05 '11 at 17:47
  • 1
    @Darin these days everyone who has access to internet has access to compiler: http://ideone.com/YqWd8 – Andrey Mar 05 '11 at 17:50
  • 8
    I think it's a reasonable question. What one compiler accepts, another may reject, and this doesn't really help to work out whether it's valid C and C++. –  Mar 05 '11 at 17:55
  • @Brone In this case, wouldn't any reasonable C compiler accept it and any reasonable C++ compiler reject it? It's certainly a reasonable question; I just get tired of getting excited and then clicking on a link for a question the OP could have answered on their own in about 30 seconds. – San Jacinto Mar 06 '11 at 19:59

4 Answers4

8

In C, it's valid but useless.

In C++ it's invalid. You can only specify storage class for objects and functions.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
4

Valid in C. Ill-formed in C++

In C++, specifiers extern/static can only be applied to names of objects or functions

Check out

C.1.5 Clause 7: declarations (7.1.1) ISO C++03


Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
2

No... That is not valid in C++. An alternative is (C++) : unnamed namespace

namespace 
{
   struct K {  int x; };
}

See this related topic:

Superiority of unnamed namespace over static?

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
1

http://ideone.com/YqWd8

http://ideone.com/XtHYy

Andrey
  • 59,039
  • 12
  • 119
  • 163