understanding format specifiers

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

  1. siliconwafer

    siliconwafer Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. siliconwafer

    Mike Wahler Guest

    "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
    #3
  4. 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>
    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 =----
     
    Mark McIntyre, Aug 30, 2005
    #4
  5. 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
    #5
  6. "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
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    0
    Views:
    637
  2. Ben Pfaff

    Re: C# format Specifiers Help.

    Ben Pfaff, Aug 21, 2003, in forum: C Programming
    Replies:
    0
    Views:
    406
    Ben Pfaff
    Aug 21, 2003
  3. Martin Ambuhl

    Re: C# format Specifiers Help.

    Martin Ambuhl, Aug 21, 2003, in forum: C Programming
    Replies:
    4
    Views:
    500
    Randy Howard
    Aug 22, 2003
  4. Jeff

    Re: C# format Specifiers Help.

    Jeff, Aug 21, 2003, in forum: C Programming
    Replies:
    0
    Views:
    458
  5. Peter Ammon

    Writing arguments under format specifiers

    Peter Ammon, Jul 4, 2004, in forum: C Programming
    Replies:
    4
    Views:
    307
    Mike Wahler
    Jul 6, 2004
Loading...

Share This Page