4

Is there any reason why the code below shouldn't work? (Compile as C Code)

code

#include <stdint.h>

int main()
{
    int var = 10;
    if (var == 0) return 1;
    uint8_t data;
    return 0;
}

Error messages

Error 3 error C2065: 'data' : undeclared identifier Error 2 error C2146: syntax error : missing ';' before identifier 'data' Error 1 error C2275: 'uint8_t' : illegal use of this type as an expression

Could this be a bug in vs2013?

To fix above, any of this can be done:

  • Return statement in bracets will fix this i.e. if (var == 0) { return; }
  • Delcare uint8_t data before the if statements
  • Use unsigned char instead of uint8_t
  • Compile with C++ "Compile as C++ Code" (Project properties-> C/C++ -> Advanced -> Compile As)

Update: vs2015 (vs140) does not seem to have this "bug"

LordAlgar
  • 81
  • 5
  • fyi - code compiles fine with gcc. most i get from pedantic is a warning : `ISO C90 forbids mixed declarations and code`. this only comes up if i specify pedantic in addition to std=c89.. – amdixon Sep 28 '15 at 13:08
  • 2
    VC is not C99 compiant. – too honest for this site Sep 28 '15 at 13:17
  • My VS2013 has no issues with that code at all, but VS2010 does not like it. – molbdnilo Sep 28 '15 at 13:24
  • The allowed forms of main() for this compiler can be found [here](https://msdn.microsoft.com/en-us/library/6wd819wh.aspx). Since `void main()` is not listed there, your code invokes undefined behavior. If the compiler was any good it would give a compiler error for this. – Lundin Sep 28 '15 at 13:43
  • @Lundin I removed it to reduce complexity in the original question. Added it now. – LordAlgar Sep 28 '15 at 13:56

2 Answers2

4

The Visual Studio C compiler have traditionally been very bad at following the later standards, and at one time it was even said that they would not support C99 (or later) at all IIRC. That means the C compiler only supports C89 which doesn't allow you to place variable declarations anywhere, only at the beginning of blocks.

So you need to do

int var = 10;
uint8_t data;   /* Moved declaration here */
if (var == 0) return;

It should be noted that Microsoft have changed their stance regarding C and the later C standards, and that later versions and updates have made the compiler more up to date with current (or at least the C99) standards.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    Do you have any reference to the claim that MSVS supports *at least C99* ? AFAIK, they only support selective features of C99 (not all). – P.P Sep 28 '15 at 13:18
  • @BlueMoon Well [this post](http://stackoverflow.com/a/27827416/440558) seems to be answering most questions about VS C99 support, one way or another. But like I said, this is a very recent effort. – Some programmer dude Sep 28 '15 at 13:20
  • 4
    It should be noted that when MS says "Visual Studio 2015 fully implements C99" they actually mean "here's some half-hearted crap where some things are missing and others don't follow the standard, because we don't know and/or care about the C standard". – Lundin Sep 28 '15 at 13:37
  • 1
    This is included in the original question. "Delcare uint8_t data before the if statements". – LordAlgar Sep 28 '15 at 13:59
1

Does VS2013 implement/claim C99 conformance? IIRC it does not. In C89, declarations must appear at the beginning of blocks and cannot be mixed with code as they can in C++ or C99 and later.

And you are not allowed to declare main as you wish. It must be int main(void).

Jens
  • 69,818
  • 15
  • 125
  • 179