KKC
KKC

Reputation: 41

C program compilation with VC++ [2005]

I can see that my VC++ 2005 express edition compiles following code perfectly

/*Following Code compiles fine*/
#include <stdio.h>

main()
{
    int i;
    int;
    5;

    printf("\n\r Whats up??");
    getchar();
    return 0;
}

However I have observed that it gives compilation error for same code with slightly different order.

/*Following code gives error*/

#include <stdio.h>
main()
{
    int;
    5;

    int i;
    printf("\n\r Whats up??");
    getchar();
    return 0;
}

Position of "int i" is different in both the cases. I get error as "Error 2 error C2143: syntax error : missing ';' before 'type' on line 6.

Why is this happening?

Thanks,

Upvotes: 1

Views: 59

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

This is happening because it seems the compiler has a bug. Neither the first program nor the second program shall be compiled.

I can guess that the second program is not compiled because declarations in old C must be placed in the beginning of a code block.

I have no at hand the old C Standard but according to the current C Standard (6.7 Declarations)

2 A declaration other than a static_assert declaration shall declare at least a declarator (other than the parameters of a function or the members of a structure or union), a tag, or the members of an enumeration.

It seems that the old C allowed empty declarations. At least in the Rationale for International Standard— Programming Languages— C Revision 2 20 October 1999 there is written:

6.7 Declarations 25 The C89 Committee decided that empty declarations are invalid, except for a special case with tags (see §6.7.2.3) and the case of enumerations such as enum {zero,one}; (see §6.7.2.2). While many seemingly silly constructs are tolerated in other parts of the language in the interest of facilitating the machine generation of C, empty declarations were considered sufficiently easy to 30 avoid.

Take into account that some compiler implementations call such bugs as their own language extensions.:)

Upvotes: 3

Related Questions