Re: understanding format specifiers
- From: Martin Ambuhl <mambuhl@xxxxxxxxxxxxx>
- Date: Tue, 30 Aug 2005 17:25:32 GMT
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;
}
.- References:
- understanding format specifiers
- From: siliconwafer
- understanding format specifiers
- Prev by Date: Re: Why C/C++ errors are SO obscure/devious??
- Next by Date: Re: Why C/C++ errors are SO obscure/devious??
- Previous by thread: Re: understanding format specifiers
- Next by thread: Re: understanding format specifiers
- Index(es):
Relevant Pages
|