Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > understanding format specifiers

Reply
Thread Tools

understanding format specifiers

 
 
siliconwafer
Guest
Posts: n/a
 
      08-30-2005
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

 
Reply With Quote
 
 
 
 
Ralf Schallenberg
Guest
Posts: n/a
 
      08-30-2005
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
 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      08-30-2005

"siliconwafer" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      08-30-2005
On 30 Aug 2005 05:35:14 -0700, in comp.lang.c , "siliconwafer"
<(E-Mail Removed)> 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 =----
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      08-30-2005
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;
}
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      08-31-2005
"Martin Ambuhl" <(E-Mail Removed)> wrote in message
news:gE0Re.4211$(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with org.apache.log4j.PatternLayout format specifiers? Pep Java 0 10-11-2005 09:45 AM
Writing arguments under format specifiers Peter Ammon C Programming 4 07-06-2004 03:41 PM
Re: C# format Specifiers Help. Martin Ambuhl C Programming 4 08-22-2003 01:57 AM
Re: C# format Specifiers Help. Jeff C Programming 0 08-21-2003 01:44 AM
Re: C# format Specifiers Help. Ben Pfaff C Programming 0 08-21-2003 12:46 AM



Advertisments