11

I'm writing C in Visual Studio 2010. The compiler doesn't seem to want to let me use inline variable declarations. The following code produces an error:

unsigned int fibonacci_iterative(unsigned int n) {
 if (n == 0) {
  return 0;
 }
 if (n == 1) {
  return 1;
 }

 unsigned int prev_prev = 0; // error
 unsigned int prev = 1; // error
 unsigned int next = 0; // error
 for (int term_number = 0; term_number < n; term_number++) {
  unsigned int temp = prev_prev + prev;
  prev = next;
  prev_prev = prev;
  next = temp;
 }
 
 return next;
}

Error:

error C2143: syntax error : missing ';' before 'type'

error C2143: syntax error : missing ';' before 'type'

error C2143: syntax error : missing ';' before 'type'

Why is this happening? Is there a setting to make the compiler not so strict?

Community
  • 1
  • 1
Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

3 Answers3

10

Putting declarations after non-declarations isn't allowed in C89, although it is allowed in C++ and in C99 (MSVC still doesn't support C99, though).

In C89, you can achieve a similar effect by using a nested block:

unsigned int fibonacci_iterative(unsigned int n) {
    if (...) {
    }

    {
       unsigned int prev_prev = 0;
       unsigned int prev = 1;
       unsigned int next = 0;
       ...
    }
 }
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • 2
    A bit messy though, isn't it? All the rest of the function that references those variables will have to be inside the block so that the variables don't go out of scope. If overused, this will lead to _very_ messy and brace-heavy code. – Chinmay Kanchi Jan 31 '10 at 02:54
  • @Chinmay: It depends. Yeah, it could end being messy for large blocks, but it also can be useful for small blocks where some variables need only a very limited scope. – jamesdlin Jan 31 '10 at 03:07
  • How portable is that? That seems like it might be asking for trouble. – Joel B Jun 06 '12 at 18:29
  • @JoelB: It's standard C89, so it should be completely portable. – jamesdlin Jun 06 '12 at 20:44
1

Try to compile it as C++. C99 will allow variables to be declared other than the top of scope, but VC doesn't know about C99.

Arthur Kalliokoski
  • 1,627
  • 12
  • 12
  • As Chinmay mentions, C and C++ aren't fully compatible at the source level. http://en.wikipedia.org/wiki/Compatibility_of_C_and_C++ – ephemient Jan 31 '10 at 06:59
1

Inline variable declarations are only supported in a C99 compliant compiler. Microsoft Visual C++ doesn't seem to be C99-compliant (ref). You're either going to have to use a C99-compliant compiler (GCC will compile code with inline variables when used with -std=c99) or declare your variables at the top of the function.

EDIT: Or try C++ mode, as suggested by akallio, though C and C++ aren't strictly compatible with each other, so if (for example), you use any C++-specific keywords in your C code, it won't compile.

Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
  • 1
    Keyword aside, different rules about pointer casting and `enum/struct/union` namespaces can also make safe C code fail to compile in C++ mode. There's other tidbits, like `sizeof('a')==sizeof(int)` in C but `sizeof('a')==sizeof(char)` in C++, but in practice that's not a common cause of problems. – ephemient Jan 31 '10 at 06:56