From: j DOT aldrich6 AT genie DOT com Message-Id: <199607140743.AA195900221@relay1.geis.com> Date: Sun, 14 Jul 96 07:18:00 UTC 0000 To: norbertj AT panix DOT com Cc: djgpp AT delorie DOT com Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=us-ascii Subject: Re: Scanf expects 4 bytes. Reply to message 0454773 from NORBERTJ AT PANI on 07/13/96 6:50PM >main() >{ > unsigned char x1,x2; > > printf("\nEnter two hex numbers 00-ff: "); > scanf("%x %x",&x1,&x2); > printf("\nx1=%02X\nx2=%02X",x1,x2); >} > >Enter two hex numbers 00-ff: 2 3 >x1=00 >x2=03 > >This is from a MS C text. Really? I'm impressed that MS hires such quality people to write its documentation... my pet poodle could do better (assuming that I had one, of course). :) Anyway... When scanf sees a '%x' specifier, it expects to put the resulting hex number into an int variable, which is 4 bytes long. Your code fragment passes it two char variables, each of which is only 1 byte long. The result is, quite obviously, not what you would want. You should compile with -Wall so that gcc will tell you when you're doing things like that and warn you about it. The correct code fragment to do what that one tries to do would look like this (on a 32-bit compiler,that is): int main( void ) /* This is the right way to define main--please use it */ { unsigned int x1,x2; printf("\nEnter two hex numbers 00000000-ffffffff: "); scanf("%x %x",&x1,&x2); printf("\nx1=%02X\nx2=%02X",x1,x2); } On a 16-bit compiler (MS C w/o extensions, Turbo C), your range would be limited to '0000-ffff'. There is *NO WAY* to input a one-byte value as a hexadecimal number, no matter what your tutorials/manuals may tell you, because '%x' only works with ints. You would have to design such code manually. (Read a 2-character string, sscanf it into an int variable, then truncate the int into a char.) John