* printf format specifiers
@ 2002-10-20 5:52 Steve Dondley
2002-10-20 7:14 ` Der Herr Hofrat
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Steve Dondley @ 2002-10-20 5:52 UTC (permalink / raw)
To: gcc-help
Hi,
I have the following line in one of my programs:
printf("%d\n", *string);
*string is a pointer to a string. The above line prints out the ASCII
decimal equivalent of the character that the pointer is pointing to. This
is what I was looking to accomplish.
My question is why? Why wouldn't I need to use the %hu (unsigned short
integer) format specifier? When I do use the %hu, I get precisely the same
results. This despite the fact that %d reads an entire word and %hu reads a
single byte.
Is this some compiler magic going on here?
Thanks.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf format specifiers
2002-10-20 5:52 printf format specifiers Steve Dondley
@ 2002-10-20 7:14 ` Der Herr Hofrat
2002-10-21 6:33 ` John Love-Jensen
2002-10-23 18:38 ` Segher Boessenkool
2 siblings, 0 replies; 5+ messages in thread
From: Der Herr Hofrat @ 2002-10-20 7:14 UTC (permalink / raw)
To: Steve Dondley; +Cc: gcc-help
> Hi,
>
> I have the following line in one of my programs:
>
> printf("%d\n", *string);
>
> *string is a pointer to a string. The above line prints out the ASCII
> decimal equivalent of the character that the pointer is pointing to. This
> is what I was looking to accomplish.
>
> My question is why? Why wouldn't I need to use the %hu (unsigned short
> integer) format specifier? When I do use the %hu, I get precisely the same
> results. This despite the fact that %d reads an entire word and %hu reads a
> single byte.
I guess you would see a difference if you would use ascii chars above 127 as
the one would change sign the other would not - and hu would read a short
which is 2bytes instead of 4 but I don_t see why %hu should read one byte
only.
hofrat
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf format specifiers
2002-10-20 5:52 printf format specifiers Steve Dondley
2002-10-20 7:14 ` Der Herr Hofrat
@ 2002-10-21 6:33 ` John Love-Jensen
2002-10-23 18:38 ` Segher Boessenkool
2 siblings, 0 replies; 5+ messages in thread
From: John Love-Jensen @ 2002-10-21 6:33 UTC (permalink / raw)
To: Steve Dondley, gcc-help
Hi Steve,
Let's assume that char is 8-bit, short is 16-bit, int is 32-bit, long is
64-bit, and long long is 128-bit.
When you put a char or short on the stack, they are expanded to int.
The int represents the platform's natural, native size... sometimes called a
"word". Note: "word" is platform dependent!
So if you had these:
char c = '1'; // 0x31, 8-bit, a quarter-word
short s = 0x1122; // 16-bit, a half-word
int i = 0x33445566; // 32-bit, a word
long d = 0x9988776655443322; // 64-bit, a double-word
long long q = 0xFFEEDDCCBBAA99887766554433221100; // 128-bit, a quad-word
If you do this:
printf("%d %d %d %ld %lld", c, s, i, d, q);
The stack will look like this:
02100010 : pointer to "%d %d %d %ld %lld"
00000031 : c, expanded to word-size
00001122 : s, expanded to word-size
33445566 : i
99887766 : d's MSW
55443322 : d's LSW
FFEEDDCC : q's MSW
BBAA9988 : q's 2SW
77665544 : q's 3SW
33221100 : q's LSW
The fictional platform presented is a big-endian platform. Ala Motorola
680x0.
--Eljay
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf format specifiers
2002-10-20 5:52 printf format specifiers Steve Dondley
2002-10-20 7:14 ` Der Herr Hofrat
2002-10-21 6:33 ` John Love-Jensen
@ 2002-10-23 18:38 ` Segher Boessenkool
2002-10-23 19:01 ` Roberto Díaz
2 siblings, 1 reply; 5+ messages in thread
From: Segher Boessenkool @ 2002-10-23 18:38 UTC (permalink / raw)
To: Steve Dondley; +Cc: gcc-help
Hi Steve,
> I have the following line in one of my programs:
>
> printf("%d\n", *string);
>
> *string is a pointer to a string. The above line prints out the ASCII
> decimal equivalent of the character that the pointer is pointing to. This
> is what I was looking to accomplish.
>
> My question is why? Why wouldn't I need to use the %hu (unsigned short
> integer) format specifier? When I do use the %hu, I get precisely the same
> results. This despite the fact that %d reads an entire word and %hu reads a
> single byte.
>
> Is this some compiler magic going on here?
printf() is declared something like
int printf(const char *format, ...);
The ellipsis (dots) here means the function takes "variable arguments".
All arguments passed "through the ellipsis" are subject to the default integer
conversions:
- if all values representable by the type of the argument are representable
by the type "int", the argument is converted to "int";
- otherwise, if all values representable by the type of the argument are
representable by the type "unsigned int", the argument is converted to
"unsigned int";
- otherwise, the argument is passed as-is.
In your case, "string" is a pointer to char, so *string is a char, which matches
the first case above, so it is passed as int, so the format specifier "%d" works
just fine. If you use "%hu", printf() still reads an int, but converts it to
an unsigned short. Now either you didn't test this with negative char values,
or your char happens to be an unsigned type; otherwise, "%hu" wouldn't have
given the same output as "%d".
Btw, if you wanted to print it as a char value, instead of a short value, you
should have used "%hhd" or "%hhu".
Hope this clarifies things for you,
Segher
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf format specifiers
2002-10-23 18:38 ` Segher Boessenkool
@ 2002-10-23 19:01 ` Roberto Díaz
0 siblings, 0 replies; 5+ messages in thread
From: Roberto Díaz @ 2002-10-23 19:01 UTC (permalink / raw)
To: Segher Boessenkool; +Cc: Steve Dondley, gcc-help
[-- Attachment #1: Type: text/plain, Size: 1007 bytes --]
There was a list called tuxcprograming or something like that for C
programming doubts.. I am mention this because I see there are at times
some C programming doubts answered in this list which is supposed to be
a gcc list..
Anyway I dont care, it doesnt bother me this kind of messages.. but
maybe other do.
> In your case, "string" is a pointer to char, so *string is a char, which matches
> the first case above, so it is passed as int, so the format specifier "%d" works
> just fine. If you use "%hu", printf() still reads an int, but converts it to
> an unsigned short. Now either you didn't test this with negative char values,
> or your char happens to be an unsigned type; otherwise, "%hu" wouldn't have
> given the same output as "%d".
>
> Btw, if you wanted to print it as a char value, instead of a short value, you
> should have used "%hhd" or "%hhu".
>
> Hope this clarifies things for you,
>
>
> Segher
>
>
>
--
gnupg public key at: wwwkeys.pgp.net
[-- Attachment #2: Esta parte del mensaje esta firmada digitalmente --]
[-- Type: application/pgp-signature, Size: 232 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-10-24 2:01 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-20 5:52 printf format specifiers Steve Dondley
2002-10-20 7:14 ` Der Herr Hofrat
2002-10-21 6:33 ` John Love-Jensen
2002-10-23 18:38 ` Segher Boessenkool
2002-10-23 19:01 ` Roberto Díaz
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).