printf format

K

Keep Asking

Can anyone tell me why the printf behave differently when I tried to
print out the hex value? Where comes from these extra "f"s?

int main()
{
char array[] = {0x7f,0x55,0x86,0x91};
int i=0;

for(i=0; i<4; i++)
{
printf("%02x\n", (unsigned char)array);
}

}

output:
7f
7f
55
55
86
ffffff86
91
ffffff91
 
M

Malcolm McLean

Can anyone tell me why the printf behave differently when I tried to
print out the hex value? Where comes from these extra "f"s?

      printf("%02x\n", (unsigned char)array);

printf() expects an unsigned int not an unsigned char for the %x
format. Without a prototype you might have subverted the promotion
system (normally the char would be promoted to an int because passed
to a variadic function, but without the prototype it doesn't know that
prinitf is variadic).However the behaviour is very odd and I expect
you've made some mistake in cutting and pasting the code.
 
T

Tom St Denis

Can anyone tell me why the printf behave differently when I tried to
print out the hex value? Where comes from these extra "f"s?

int main()
{
  char array[] = {0x7f,0x55,0x86,0x91};
  int i=0;

  for(i=0; i<4; i++)
    {
      printf("%02x\n", (unsigned char)array);
    }

}

output:
7f
7f
55
55
86
ffffff86
91
ffffff91


How do you get 8 lines of output from this?

tstdenis@photon:~$ gcc -W -Wall test.c -o test
test.c: In function 'main':
test.c:7: warning: implicit declaration of function 'printf'
test.c:7: warning: incompatible implicit declaration of built-in
function 'printf'
test.c:9: warning: control reaches end of non-void function
tstdenis@photon:~$ ./test
7f
55
86
91

Your program has bugs in it, and despite that it prints out what you'd
expect on my platform at least. You can "keep asking" all you want.

Tom
 
E

Eric Sosman

Can anyone tell me why the printf behave differently when I tried to
print out the hex value? Where comes from these extra "f"s?

int main()
{
char array[] = {0x7f,0x55,0x86,0x91};
int i=0;

for(i=0; i<4; i++)
{
printf("%02x\n", (unsigned char)array);
}

}

output:
7f
7f
55
55
86
ffffff86
91
ffffff91


The only was I can explain this output is to suppose that
you've linked the program with your own spurious version of
printf(), just to try to fool everyone.

If you haven't done that, or something like it, then I
suspect the output did not come from the code shown: Either you
ran some other code, or you got some other output (or both).

Either way, I can't help. Sorry.
 
B

bart.c

Keep Asking said:
Can anyone tell me why the printf behave differently when I tried to
print out the hex value? Where comes from these extra "f"s?

int main()
{
char array[] = {0x7f,0x55,0x86,0x91};
int i=0;

for(i=0; i<4; i++)
{
printf("%02x\n", (unsigned char)array);
}

}

output:
7f
7f
55
55
86
ffffff86
91
ffffff91


You get the extra characters when you use an signed int, unsigned int, or no
int cast in the printf() line.

Not with an unsigned char cast. (And your output seems all mixed up.)

The extra f's are due to the signed char values being allowed to expand to
full int size before any conversions. Then they are due to negative values
in the number. Printing using %d or %u will also show the problems.

Using unsigned char for your array should fix things (signed chars always
give trouble but no-one here ever believes me).
 
K

Keep Asking

Sorry I did make a mistake when copy the program. Repost it.

Only
printf("%02x\n", (unsigned char)array);
gives what I want. I want to know why.

uname -a
Linux myang-desktop 2.6.24-26-generic #1 SMP Tue Dec 1 17:55:03 UTC
2009 x86_64 GNU/Linux

gcc --version
gcc (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There
is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.

output:
7f
7f
7f
55
55
55
86
ffffff86
ffffff86
91
ffffff91
ffffff91
 
F

Francois Grieu

Le 07/07/2010 18:27, Keep Asking a écrit :
Sorry I did make a mistake when copy the program. Repost it.

Only
printf("%02x\n", (unsigned char)array);


[not printf("%02x\n", array); ]
gives what I want. I want to know why.

What is the type of array ?

Is that signed or unsigned on your platform given the set of compile
options you use?

What is the prototype of printf?
Hint: it is in <stdio.h>, which you should include

What happens to a variable or expression of such type and signedness
when passed to a function with such prototype?


Francois Grieu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top