tags:

views:

102

answers:

3
#include <stdio.h>
#include <Windows.h>

int main()
{
    TCHAR buff[1024];
    GetLogicalDriveStrings(1024, buff);

    int i;
    for(i = 0; buff[i] != 0; i += 4)
        printf("%S", buff+i);
}

When I try to compile it with MSVC, I get the following errors:

Commenting out GetLogicalDriveStrings(1024, buff); causes the code to compile just fine

+6  A: 

Older version of C require local variables to be declared at the beginning of a block, before things like function calls. Move the int i; to the top of the function to be with the declaration of buff.

C++ did away with this requirement, as did C99.

Amber
@Amber: `C requires local variables to be declared at the beginning of a block, before things like function calls. ` Thats not the case with C99. I think MSVC++ still doesn't support C99.
Prasoon Saurav
Added a note about C99 in addition to the one about C++.
Amber
That's really surprising that MSVC doesn't support C99, I usually only ever use C when I'm on Linux.
Charlie Somerville
+1  A: 

Change it to:

#include <stdio.h>
#include <Windows.h>

int main()
{
    int i;
    TCHAR buff[1024];
    GetLogicalDriveStrings(1024, buff);

    for(i = 0; buff[i] != 0; i += 4)
        printf("%S", buff+i);
}

Declare variables before calling functions in C.

David Titarenco
I doubt it.. he trying to call Window function in C console program, without actually including lib file!
thatsalok
@thatsalok: He's getting compile errors, not link errors.
Ignacio Vazquez-Abrams
I know, but i doubt it will work :-)
thatsalok
A: 

move "int i" to the line before or after "TCHAR buff[1024]", or rename your main.c to main.cpp

btw, you should use _t series functions/macros once you decided to use TCHAR:

_tprintf(_T("..."))

Jason