October 27, 2024
Chicago 12, Melborne City, USA
C#

unsigned charS and scanf in C


The input accepts numbers from 0 to 255, which must be stored in variables a and b, which are both unsigned char.

My code:

int main(void)
{
    unsigned char a, b;
    int res = scanf("%hhu %hhu", &a, &b);
    printf("res = %d, a = %u, b = %u\n", res, a, b);
    return 0;
}

The problem is that the first variable is never assigned a value. It is always zero:

input: 7 8
output: res = 2, a = 0, b = 8

The most interesting thing is that if you change the char type to short, everything starts working. Also, if you read each variable separately with a scan. What’s the problem?



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video