Re: understanding format specifiers



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;
}
.



Relevant Pages

  • Re: reading in and parsing through a binary file
    ... The fact that you say you read the file into a "char buffer" belies ... I know what the format is and understand the format. ... Example being first two bytes (in hex) say 01 40 ... would need to be translated into 320 and stored in an int variable ...
    (comp.lang.c)
  • Re: Give some ideas for c Optimzation
    ... > please give some optimization techiques for this routine and also give ... > 1.complier optimzation ... > int hextodecimal ... The most important point is that no one wants to call a hex to decimal ...
    (comp.lang.c)
  • Re: Sring in Bytearray
    ... Beim Inhalt in dem String handelt es sich um ein Hexvalue das dann auch genauso im file landen soll. ... z.b Hex 40 bleibt hex 40 egal ob ASCII oder EBCDIC. ... Es wird eine länge berechnet die von int in ein entsprechendes Hex value umgewandelt werden muss das dann in eine Datei geschrieben wird. ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Converting char* to char
    ... Calling an int a byte is misleading; ... I recommend you use C++ Standard objects and functions to do ... Alternatively you could use the C function strtoul() in ... which can auto-detect decimal or hex. ...
    (comp.lang.cpp)
  • Re: understanding format specifiers
    ... > I will get hex equivalent of 43. ... > Does the format specifier do an implicit "decimal to hex" conversion ... It does an explicit conversion from ...
    (comp.lang.c)