boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

200-lex-char-type.c (882B)


      1 /* C99 §6.4.4.4: "An integer character constant has type int."
      2  *
      3  * Therefore sizeof('A') must equal sizeof(int), not sizeof(char).
      4  * Also the value of '\xFF' as a character constant has type int and,
      5  * when its impl-defined value is non-negative (here 255), should
      6  * compare equal to the int 255 — a char-typed constant would
      7  * sign-extend to -1 instead.
      8  */
      9 
     10 int test_sizeof(void) {
     11     if (sizeof('A') != sizeof(int)) return 1;
     12     if (sizeof('A') == sizeof(char)) return 2;
     13     return 0;
     14 }
     15 
     16 int test_value(void) {
     17     /* '\xFF' must have int rank; promotion of an int 255 keeps 255. */
     18     int x = '\xFF';
     19     if (x == -1) return 1;   /* would happen if typed as signed i8 */
     20     if (x != 255) return 2;
     21     return 0;
     22 }
     23 
     24 int main(int argc, char **argv) {
     25     int r;
     26     if ((r = test_sizeof())) return 10 + r;
     27     if ((r = test_value()))  return 20 + r;
     28     return 0;
     29 }