By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,956 Members | 1,457 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,956 IT Pros & Developers. It's quick & easy.

understanding format specifiers

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 15 '05 #2

P: n/a

"siliconwafer" <sp*********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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
Nov 15 '05 #3

P: n/a
On 30 Aug 2005 05:35:14 -0700, in comp.lang.c , "siliconwafer"
<sp*********@yahoo.com> 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>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== 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 =----
Nov 15 '05 #4

P: n/a
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;
}
Nov 15 '05 #5

P: n/a
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:gE*****************@newsread2.news.atl.earthl ink.net...
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.
Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.