Jump to content
  • Advertisement
Sign in to follow this  
DevFred

C99 conformance

This topic is 3607 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am currently trying to figure out what C99 features certain compilers support. Could you be so kind to try and compile the following code on your favorite compiler with C99 mode enabled and tell me what doesn't compile? I am especially interested in recent Microsoft compilers.
#include <stdio.h>
#include <stdbool.h>
#define my_printf(...) printf(__VA_ARGS__)

void comment(void)
{
    // single line comment
}

void for_loop(void)
{
    for (int i = 0; i < 10; ++i);
}

void late_declarations(void)
{
    printf("before\n");
    int i = 5;
    printf("after\n");
}

void boolean()
{
    _Bool f = 0;
    bool t = true;
}

const int global = 10;
void const_arrays(void)
{
    const int local = 20;
    int a[global];
    int b[local];
}

void simple_vla(int n)
{
    int a[n];
}

void foo(int *p)
{
    while (*p) printf("%d", *p++);
    printf("\n");
}

void compound_literal(void)
{
    foo((int []){ 1, 2, 3, 0 });
}

void designated_initializers(void)
{
    int array[10] = { [2] = 9, [9] = -1 };
    struct s {int a; int b; int c;} t = { .b = 5 };
}

int main(void)
{
    comment();
    for_loop();
    late_declarations();
    boolean();
    const_arrays();
    simple_vla(10);
    compound_literal();
    designated_initializers();
    my_printf("variadic macros ftw!\n");
}

Thanks a lot. I am using gcc 3.4.5 with the --std=c99 option and there are no compiler errors.

Share this post


Link to post
Share on other sites
Advertisement
Visual Studio 2008 Standard doesn't seem to include stdbool.h. Removing the include and the boolean test leads to a bunch of errors. It doesn't seem to support C99 at all (by default; I'm not aware of an equivalent to --std=c99, but if there is one, then obviously you'll want to ignore this!).

Share this post


Link to post
Share on other sites
VS supports some of C99 via extensions (that presumably also work in C++). Obviously there are pros and cons to that approach; their stance, to my understanding, is that they won't bother adding it unless there is enough demand.

Share this post


Link to post
Share on other sites
What about for_loop(), late_declarations() and const_arrays()? I would really like to hear that MS supports them...

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
Could you post the complete error messages, please?


Sure. I genuinely mean that it appears to have no support whatsoever, though:

main.c(12) : error C2143: syntax error : missing ';' before 'type'
main.c(12) : error C2143: syntax error : missing ';' before 'type'
main.c(12) : error C2143: syntax error : missing ')' before 'type'
main.c(12) : error C2143: syntax error : missing ';' before 'type'
main.c(12) : error C2065: 'i' : undeclared identifier
main.c(12) : warning C4552: '<' : operator has no effect; expected operator with side-effect
main.c(12) : error C2059: syntax error : ')'
main.c(12) : error C2065: 'i' : undeclared identifier
main.c(18) : error C2143: syntax error : missing ';' before 'type'
main.c(32) : error C2057: expected constant expression
main.c(32) : error C2466: cannot allocate an array of constant size 0
main.c(32) : error C2133: 'a' : unknown size
main.c(33) : error C2057: expected constant expression
main.c(33) : error C2466: cannot allocate an array of constant size 0
main.c(33) : error C2133: 'b' : unknown size
main.c(38) : error C2057: expected constant expression
main.c(38) : error C2466: cannot allocate an array of constant size 0
main.c(38) : error C2133: 'a' : unknown size
main.c(49) : error C2059: syntax error : '{'
main.c(54) : error C2059: syntax error : '['
main.c(55) : error C2059: syntax error : '.'

Share this post


Link to post
Share on other sites
From that list recent Visual Studio versions at least support variadic macros, and single-line comments of course. Still you would have thought they could have gotten around to adding <stdint.h> and <stdbool.h> by now. Something is seriously wrong when my C64 C compiler has better C99 support than Microsoft's compiler suite..

Also with all the obsession about "safe" library functions you'd think they could implement a proper terminating snprintf, instead of adding yet another non-standard version and accompanying ifdef mess to worry about..

But for the most part they've got the important bits (restrict for instance), for the rest I can manage with slightly less convenient workarounds and custom library functions.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevFred
What about for_loop(), late_declarations() and const_arrays()? I would really like to hear that MS supports them...


I can't imagine how const_arrays() could possibly fail on a compiler whenever simple_vla() works. After all, const int is convertible (not in-place of course!) to int, so you could always just get a VLA out of it. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie
VS supports some of C99 via extensions (that presumably also work in C++). Obviously there are pros and cons to that approach; their stance, to my understanding, is that they won't bother adding it unless there is enough demand.

Yup according to Microsoft there doesn't seem to be much interest or at least there isn't interest from their biggest customers anyways...

"
Now, the Visual C++ compiler team receives the occasionally question as to why we haven’t implemented C99. It’s really based on interest from our users. Where we’ve received many requests for certain C99 features, we’ve tried to implement them (or analogues). A couple examples are variadic macros, long long, __pragma, __FUNCTION__, and __restrict. If there are other C99 features that you’d find useful in your work, let us know! We don’t hear much from our C users, so speak up and make yourselves heard J
"


I wouldn't hold my breath for much additional C99 conformance from Microsoft anytime soon since I recall a post by a programmer there predicting 80 bit long doubles would make it into VS2008 years ago and it didn't:(


Why don’t we support extended precision in 8.0? I wish I could give a satisfactory answer. The decision was made before I started working on floating-point. The truth is that even had I and others insisted upon it, I seriously doubt the feature would have made the final cut. To support 80bit long doubles we’d have to implement several high-risk, costly changes to both the front-end and the optimizer.

However, the underlying reason is this: customers are not complaining about the lack of 80bit long doubles nor are they demanding it. True, William Kahan and other numerical VIPs have long criticized Microsoft for the lack of 80bit long doubles. However influential these people may be in their industry, it’s not they but our customers who largely determine which features get incorporated and which features get postponed. To my knowledge, VC's customers are not complaining about the lack of 80bit long doubles, nor are they demanding such support. Nor have trade journals and magazines criticized the product for lacking 80bit long doubles. The Visual C++ product managers take customer feedback very (very) seriously as they should. They would argue that if customers don't want the feature, why should we add it?

I argue that customers do want the feature, they just don't know it yet. If enough customers were to complain about the lack of 80bit long doubles and start demanding it, I feel quite confident that we’d get the feature. (Hint Hint!) I also contend that were we to add 80bit long doubles, more numerical programmers would start using our product. Unfortunately, I don’t have enough market analysis information to fully support this claim (my evidence is only anecdotal).

The upside is that for the next version of the product (VC9.0 that is), we will probably support 80 bit long doubles.


Anyways if you want your code to compile under VS2008 you have to comment out quite a bit as this code now compiles as shown here:

#include <stdio.h>
//#include <stdbool.h>
#define my_printf(...) printf(__VA_ARGS__)

void comment(void)
{
// single line comment
}

void for_loop(void)
{
for (int i = 0; i < 10; ++i);
}

void late_declarations(void)
{
printf("before\n");
int i = 5;
printf("after\n");
}

void boolean()
{
bool f = 0;
bool t = true;
}

const int global = 10;
void const_arrays(void)
{
const int local = 20;
int a[global];
int b[local];
}

/*
void simple_vla(int n)
{
int a[n];
}
*/


void foo(int *p)
{
while (*p) printf("%d", *p++);
printf("\n");
}

/*
void compound_literal(void)
{
foo((int []){ 1, 2, 3, 0 });
}
*/


/*
void designated_initializers(void)
{
int array[10] = { [2] = 9, [9] = -1 };
struct s {int a; int b; int c;} t = { .b = 5 };
}
*/


int main(void)
{
comment();
for_loop();
late_declarations();
boolean();
const_arrays();
//simple_vla(10);
//compound_literal();
//designated_initializers();
my_printf("variadic macros ftw!\n");
}


Share this post


Link to post
Share on other sites
The simple answer is that Micorsoft don't produce a C99 compiler. They produce a C++ compiler with a C mode that approximates C89.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!