ProgrammingVery hardTCS NQT · Accenture

How many of these statements are true when x = 255? Count them with this program.

How many of these statements are true when x = 255? Count them with this program.
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint8_t x = 255;
    int c = 0;
    if ((uint8_t)(x + 1) > x) c++;
    if ((uint8_t)~x == 255 - x) c++;
    if (x * 1 == x) c++;
    printf("%d", c);
}
  1. A. 3
  2. B. 1
  3. C. 0
  4. D. 2

Correct answer: D. 2

For x = 255 the first test fails because the sum wraps to 0, and the other two hold. So two of the three statements are true at this value of x.

More C Programming questions

Exams that ask this topic