[Info-vax] C compiler question
John Reagan
xyzzy1959 at gmail.com
Mon Jul 29 09:11:05 EDT 2019
On Monday, July 29, 2019 at 9:05:12 AM UTC-4, John E. Malmberg wrote:
> On 7/27/2019 11:57 AM, Mark Berryman wrote:
> > Something I see all the time when porting unix code is something similar
> > to the following:
> >
> > unsigned int x;
> > .
> > .
> > .
> > if (x <= 0) {something}
> >
> > This, of course, generates a QUESTCOMPARE informational. I'm wondering
> > what code the compiler generates in such a case. Does it treat the
> > variable as if it had been declared signed or does it do something else?
> > Is the compiler's behavior in this case defined by a standard or is it
> > left up to the implementation?
>
> By the standard. "x" gets promoted to a signed type which preserves the
> negative value.
>
> This is unavoidable in porting code since many standard routines are
> allowed to have a prototype return an unsigned type, yet specify a
> negative value for errors.
>
> > Part of what I am wondering is if I can count on the code generated by a
> > VMS compiler to function the same as the code generated by a gcc or
> > clang compiler.
>
> I had the same concerns until the relevant section of the C standard was
> shown to me. I think it was on comp.os.vms that I may have asked about
> it a few years ago.
>
> Regards,
> -John
> wb8tyw at qsl.net_work
C's unsigned promotion rules are rarely what a human expects. Go spend 6 minutes and watch
https://www.youtube.com/watch?v=wvtFGa6XJDU
If that still doesn't scare you away, go spend an hour and watch
https://www.youtube.com/watch?v=yG1OZ69H_-o
More information about the Info-vax
mailing list