understanding format specifiers

S

siliconwafer

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
 
R

Ralf Schallenberg

siliconwafer said:
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
 
M

Mike Wahler

siliconwafer said:
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
 
M

Mark McIntyre

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....
 
M

Martin Ambuhl

siliconwafer said:
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;
}
 
C

Charlie Gordon

Martin Ambuhl said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top