3

Could anyone please explain why this code compiles :

#include <stdio.h>
#include <string.h>

int main (int argc, char *argv [])
{
    FILE *ptr;

    char string[10] = "Testing";

    ptr = fopen("C:\\Users\\Jordan\\Desktop\\Hello.txt", "wb");

    fwrite(string,sizeof(string[0]), sizeof(string)/sizeof(string[0]), ptr);
}

Yet this does not : Gives an Error C2065:'string' : undeclared identifer

#include <stdio.h>
#include <string.h>


int main (int argc, char *argv [])
{
    FILE *ptr;

    ptr = fopen("C:\\Users\\Jordan\\Desktop\\Hello.txt", "wb");

    char string[10] = "Testing";

    fwrite(string,sizeof(string[0]), sizeof(string)/sizeof(string[0]), ptr);

}

I am using Visual Studio 2010 on a Windows 7 Machine.

Thanks

JohnFx
  • 34,542
  • 18
  • 104
  • 162
Jordan
  • 531
  • 2
  • 7
  • 17
  • @DavidHeffernan - Yes , when I compile and run he former piece of code it works. Yet when I try to compile the bottom version ( Where my string is declared after opening the file it gives me this error. Error 3 error C2065: 'string' : undeclared identifier – Jordan Dec 13 '11 at 22:07
  • 1
    Yeah, the others have it. Nasty MS C89! If you want to write C rather than C++ then you should get a better compiler. – David Heffernan Dec 13 '11 at 22:08
  • 1
    I love how the `string` and `string.h` confused the hell out of me (and probably everyone else as well). – Mysticial Dec 13 '11 at 22:09
  • Sadly, this is unlikely to change in the near future: http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards – Adam Rosenfield Dec 13 '11 at 22:10

3 Answers3

8

Visual Studio uses the old C89/90 C. In that older C version, you can't mix declarations and code.

All your declarations must go on top. That's why the second example fails to compile.

//  This a declaration
FILE *ptr;

//  This is code
ptr = fopen("C:\\Users\\Jordan\\Desktop\\Hello.txt", "wb");

//  This is another declaration. Not Allowed in C89/C90!!!
char string[10] = "Testing";
Mysticial
  • 464,885
  • 45
  • 335
  • 332
  • 1
    And I believe MS has gone on record stating that VS will *never* support anything beyond C89. – John Bode Dec 13 '11 at 22:08
  • Thanks! I thought something like this was happening, hard to believe! Back to eclipse... – Jordan Dec 13 '11 at 22:10
  • I don't think they've stated it explicitly, but they've definitely implied it many times over based on their responses to requests for C99 support. – Mysticial Dec 13 '11 at 22:10
  • @JohnBode: where'd they state that? They used be involved in the C1X discussion. – Fred Foo Dec 13 '11 at 22:10
  • @larsmans: See http://stackoverflow.com/questions/146381/visual-studio-support-for-new-c-c-standards for some citations. – Adam Rosenfield Dec 13 '11 at 22:11
3

In (the C89 version of) C, all variables must be declared at the top of the block (the function, in this case). In your first example, you're doing that, in your second one you're not.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
2

If you saved this file with a .c extension the compiler is interpreting it as a C source file, and since VC++ support for C is for C89, the C89 rules for variable declaration apply; in particular, in C89 you must declare all the local variables at the beginning of their block.

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299