is this a FBSD printf bug?

2003-06-21 Thread abc
FBSD 4.8

i hope this isn't a question based on extreme
ignorance - i haven't programmed in C in a
long time, and i don't have another machine
to test this on.  i can't understand why
the output of the following code produces
ints when given variables of type char,
so it looks like a bug to me ...

#include stdio.h
///
int main(int argc, char *argv[]) {

#define LEN_ARRAY 16
char a[LEN_ARRAY+1];
int i;

a[0]=0x00;
a[1]=0x11;
a[2]=0x22;
a[3]=0x33;
a[4]=0x44;
a[5]=0x55;
a[6]=0x66;
a[7]=0x77;
a[8]=0x88;
a[9]=0x99;
a[10]=0xaa;
a[11]=0xbb;
a[12]=0xcc;
a[13]=0xdd;
a[14]=0xee;
a[15]=0xff;

for ( i = 0; i  LEN_ARRAY; ++i ) printf([%02i]%02x\n, i, a[i]);
}
///
// 
// OUTPUT:
// 
// [00]00
// [01]11
// [02]22
// [03]33
// [04]44
// [05]55
// [06]66
// [07]77
// [08]ff88 ???
// [09]ff99 ???
// [10]ffaa ???
// [11]ffbb ???
// [12]ffcc ???
// [13]ffdd ???
// [14]ffee ???
// [15] ???
___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]


Re: is this a FBSD printf bug?

2003-06-21 Thread Matthew Seaman
On Sat, Jun 21, 2003 at 10:14:12AM +, [EMAIL PROTECTED] wrote:
 i hope this isn't a question based on extreme
 ignorance - i haven't programmed in C in a
 long time, and i don't have another machine
 to test this on.  i can't understand why
 the output of the following code produces
 ints when given variables of type char,
 so it looks like a bug to me ...

Nope -- this is a C language FAQ (See
http://www.faqs.org/faqs/C-faq/faq/, particularly section 15).
Neither is it FreeBSD specific.  If you pass arguments to a varargs
function, like printf(3), then they will get type promoted.  In this
case 'char' is promoted to 'int'.  The reason the results you're
seeing don't match up to your expectations is that the 'char' type is
signed, and you seem to want to be using an unsigned type.

Cheers,

Matthew


-- 
Dr Matthew J Seaman MA, D.Phil.   26 The Paddocks
  Savill Way
PGP: http://www.infracaninophile.co.uk/pgpkey Marlow
Tel: +44 1628 476614  Bucks., SL7 1TH UK


pgp0.pgp
Description: PGP signature


Re: is this a FBSD printf bug?

2003-06-21 Thread Han Hwei Woo
Hrrm... your question and the code seem to contradict each other.

Firstly, you are assigning hex values to the characters in your array.
Secondly, you are printing the values stored in the array as hex values by
specifying %02x.

I think you meant to ask why you are getting the preceding trail of
...'s. If that's the case, it's because you are assigning values to
signed character variables and printing them as unsigned hex values. Thus,
when you assign values greater than 127 (starting with 88), you are
overriding the sign of the character. To get the output you would expect,
simply declare a as an unsigned character array.

e.g. unsigned char a[LEN_ARRAY+1];

- Original Message - 
From: [EMAIL PROTECTED]
To: freebsd-questions [EMAIL PROTECTED]
Sent: Saturday, June 21, 2003 7:14 AM
Subject: is this a FBSD printf bug?


 FBSD 4.8

 i hope this isn't a question based on extreme
 ignorance - i haven't programmed in C in a
 long time, and i don't have another machine
 to test this on.  i can't understand why
 the output of the following code produces
 ints when given variables of type char,
 so it looks like a bug to me ...

 #include stdio.h


///
 int main(int argc, char *argv[]) {

 #define LEN_ARRAY 16
 char a[LEN_ARRAY+1];
 int i;

 a[0]=0x00;
 a[1]=0x11;
 a[2]=0x22;
 a[3]=0x33;
 a[4]=0x44;
 a[5]=0x55;
 a[6]=0x66;
 a[7]=0x77;
 a[8]=0x88;
 a[9]=0x99;
 a[10]=0xaa;
 a[11]=0xbb;
 a[12]=0xcc;
 a[13]=0xdd;
 a[14]=0xee;
 a[15]=0xff;

 for ( i = 0; i  LEN_ARRAY; ++i ) printf([%02i]%02x\n, i, a[i]);
 }


///
 //
 // OUTPUT:
 //
 // [00]00
 // [01]11
 // [02]22
 // [03]33
 // [04]44
 // [05]55
 // [06]66
 // [07]77
 // [08]ff88 ???
 // [09]ff99 ???
 // [10]ffaa ???
 // [11]ffbb ???
 // [12]ffcc ???
 // [13]ffdd ???
 // [14]ffee ???
 // [15] ???
 ___
 [EMAIL PROTECTED] mailing list
 http://lists.freebsd.org/mailman/listinfo/freebsd-questions
 To unsubscribe, send any mail to
[EMAIL PROTECTED]


___
[EMAIL PROTECTED] mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to [EMAIL PROTECTED]