[SystemSafety] Fwd: Re: Correcting the SECOND screw-up (grovelling apologies)

Olwen Morgan olwen at phaedsys.com
Tue Jul 14 13:36:03 CEST 2020



Even screwed up the attempted correction!

Should have formed the ch2 values using bitwise OR instead of addition:

Corrected code is:

#include <stdio.h>

/* program to test for signedness of plain char in C      */

/* assumes that all integral types have an even number of bits */

static const char diag[2][9] = { "UNSIGNED", "SIGNED" };
static const char two           = (char)2;

int main (void)
{
     int ch1 = (char)2;
     int ch2 = (char)((ch1 << 2) | two);

     printf("\nPlain char signedness test: ");

     while (ch2 != ch1)
     {
         ch1 = (char)ch2;
         ch2 = (char)((ch1 << 2) | two);
     }

     printf ("ch1 = %i, plain char is %s\n", ch1, diag[ (ch1 < 0) ]);

     return 0;
}

Just goes to show how you can mess things up in a language you've been 
using for decades ... and why CbyC tooling is your friend. I feel 
terribly exposed writing C without decent tools.

Heading for evidently necessary recaffeination,

Olwen

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.techfak.uni-bielefeld.de/pipermail/systemsafety/attachments/20200714/1521f3b1/attachment.html>


More information about the systemsafety mailing list