1
votes

So this is the code

void main()
{
  unsigned char n,t;
  scanf("%hhu %hhu",&n,&t);
  printf("%hhu %hhu",n,t);
}

The problem is when I input 5 and 1 respectively, the output is 0 and 1. 0 for n and 1 for t. However, when I changed the type from char to int/unsigned, the output is right as expected: 5 and 1.

The question is why asking for (number) input with char gives invalid value?

2
What are you trying to achieve with this? scanf is expecting unsigned char* with these formatting strings, is this intended? - bash.d
example program does not give the incorrect output you describe for me ( gcc on Ubuntu 12.04 ) - Vorsprung
May I ask which on platfrom you experience this behaviour? - alk
@bash.d (CMIIW) In addition to store a character, char can be used to save 1 byte of number (-127..128 or 0..255) right? I'm just trying it for fun, and then I'm curious about the error - user1514088
Alright! There you go - bash.d

2 Answers

3
votes

int main(void) please

scanf("%hhu %hhu",&n,&t);

here ---------- ^ ------^ unsigned char * is expected

same for printf("%hhu %hhu",n,t);

so change

char n,t;

to

unsigned char n,t;

0
votes

Besides that it is

int main(void) 

at least, you also might like to also add the necessary prototypes by including the appropriate header:

#include <stdio.h>