# understanding format specifiers

Discussion in 'C Programming' started by siliconwafer, Aug 30, 2005.

1. ### siliconwaferGuest

Hi All,
What does a 'format specifier' do?
Suppose I do,
int a = 43; //decimal number
printf("%x",a);
I will get hex equivalent of 43.
Does the format specifier do an implicit "decimal to hex" conversion
before displaying ?
Is it true for all format specifier?
-Siliconwafer

siliconwafer, Aug 30, 2005

2. ### Ralf SchallenbergGuest

siliconwafer wrote:

> What does a 'format specifier' do?
> Suppose I do,
> int a = 43; //decimal number

Only the representation of the number in the source code
is decimal. Inside the memory the number is stored in binary.

The line
int a = 0x2b;
will store the same binary number in memory.
It is not stored in hexadecimal format.

> printf("%x",a);
> I will get hex equivalent of 43.
> Does the format specifier do an implicit "decimal to hex" conversion
> before displaying ?

No, in the above example printf gives you a hexadecimal representation
of the value of variable a.

printf("%d\n", a);
prints a decimal representation of variable a.
In both cases printf has to do a "conversion".
It has to work with the value of a to get a certain
representation of this value. The format specifier
tells printf which representation you want.

regards
Ralf

Ralf Schallenberg, Aug 30, 2005

3. ### Mike WahlerGuest

"siliconwafer" <> wrote in message
news:...
> Hi All,
> What does a 'format specifier' do?
> Suppose I do,
> int a = 43; //decimal number
> printf("%x",a);
> I will get hex equivalent of 43.
> Does the format specifier do an implicit "decimal to hex" conversion
> before displaying ?

No. It does an explicit (stated with "%x") conversion from
binary to hex.

> Is it true for all format specifier?

Yes, all format specifiers cause a conversion from binary
to text.

-Mike

Mike Wahler, Aug 30, 2005
4. ### Mark McIntyreGuest

On 30 Aug 2005 05:35:14 -0700, in comp.lang.c , "siliconwafer"
<> wrote:

>Hi All,
>What does a 'format specifier' do?

tells printf how to format the output......

>Suppose I do,
>int a = 43; //decimal number
>printf("%x",a);
>I will get hex equivalent of 43.
>Does the format specifier do an implicit "decimal to hex" conversion

No, it simply prints it using hex format. Bear in mind that its
exceptionally unlikely that 43 was stored in decimal in the first
place!
>Is it true for all format specifier?

All format specifiers tell printf what format to use, yes....
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Mark McIntyre, Aug 30, 2005
5. ### Martin AmbuhlGuest

siliconwafer wrote:
> Hi All,
> What does a 'format specifier' do?

It tells the input or output routine how to interepret an encoded value.

> Suppose I do,
> int a = 43; //decimal number
> printf("%x",a);
> I will get hex equivalent of 43.
> Does the format specifier do an implicit "decimal to hex" conversion
> before displaying ?

There is no "decimal to hex conversion." The value (decimal) 43 is
stored in a manner specific to your machine, but equivalent to
0x2b or 053
probably stored as a series of binary digits
0..00101011

Interestingly, your question is just backwards. The code below probably
performs a binary to decimal conversion. Note that binary, octal, and
hex refer to human-readable strings (only octal and hex representations
are directly supported for I/O). Consider the above binary digits.
They can be grouped in at least these ways:
0..0010 1011 0..00 101 011
2 b (hex) 0 5 3 (octal)
Decimal is also only a human-readable convention, but requires a bit
more work since it can't be done just by grouping binary digits.

#include <stdio.h>
int main(void) {
int a = 43; /* probably stores a binary representation of
the value (decimal) 43 */
printf("%d\n", a) /* prints a decimal representation of
what is probably stored as a binary
value */
return 0;
}

Martin Ambuhl, Aug 30, 2005
6. ### Charlie GordonGuest

"Martin Ambuhl" <> wrote in message
news:gE0Re.4211\$...
> There is no "decimal to hex conversion." The value (decimal) 43 is
> stored in a manner specific to your machine, but equivalent to
> 0x2b or 053
> probably stored as a series of binary digits
> 0..00101011

Actually, it is *required* to do that on a conforming platform.
Note that ordering bits from most significant to least significant left to right
is merely a printing convention as well.

Chqrlie.

Charlie Gordon, Aug 31, 2005