-1

I'm having some issues with this homework question, is the output a constant string of 0's?

#include <stdio.h>

int main()
{
    unsigned char x;
    for (x = 0; x <= 0xFF; x++) {
        printf("0");
    }
    printf("1\n");
}
Natch
  • 1
  • 1
    Undecidable because the size of `char` in the environment is not speficied. – MikeCAT Feb 23 '21 at 10:02
  • [Yes on all Posix archs.](https://en.wikipedia.org/wiki/Character_(computing)#char) [No on some others.](https://stackoverflow.com/questions/32091992/is-char-bit-ever-8) –  Feb 23 '21 at 10:11
  • @dratenik the CHAR_BIT will tell you the truth :) – 0___________ Feb 23 '21 at 10:16
  • When exactly did you expect the unsigned char to _not_ have a value between 0 and 0xFF? I mean if this code was written for some obsolete weirdo DSP then the size of char would be the least of your problems... notably, you wouldn't be programming a PC no longer so the rest of main() wouldn't make much sense. – Lundin Feb 23 '21 at 12:03

1 Answers1

1

You can check what will happen by checking the size of the character in bits:

int main(void)
{
    unsigned char x;
    #if CHAR_BIT > 8
    for (x = 0; x <= 0xFF; x++) {
        printf("0");
    }
    printf("1\n");
    #else 
    printf("This loop will never end\n");
    #endif
}
0___________
  • 60,014
  • 4
  • 34
  • 74