473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

understanding format specifiers

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

Nov 15 '05 #1
5 2442
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
Nov 15 '05 #2

"siliconwafer" <sp*********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr 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
Nov 15 '05 #3
On 30 Aug 2005 05:35:14 -0700, in comp.lang.c , "siliconwafer"
<sp*********@yahoo.com> 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 =----
Nov 15 '05 #4
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;
}
Nov 15 '05 #5
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:gE*****************@newsread2.news.atl.earthl 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.
Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
2
by: Ravi Uday | last post by:
HI, What does these mean - - " lx%08lx " - " %1024%* " I have seen them being used in printfs. When do you exactly write/use these ? Thx in advance
5
by: nimdez | last post by:
Hi, I am working on an existing code base in which a lot of data displayed to the user is formatted in tables. Most tables are printed row-by-row using printf() with "%s" print conversion...
11
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <=...
1
by: Chris Morse | last post by:
Hi, I've been trying to figure out where in the documentation it describes all the String.Format() formatting specifiers. So far, I've been guessing and picking up specifiers in sample code.....
6
by: Udi | last post by:
Hi, I tried looking for it in the documentation but with no luck. What's the equivalent C control strings in C# for the following: "%3d" ? (space padding) "%-3d" ? (left alignment) "%+3d" ?...
3
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is...
2
by: rh00667 | last post by:
hi all, seeing a source i found these printf format specifiers (new for me) %m and %n$ i tested and works nice, it seems that they are not documented in man (???) i would appreciate to...
1
by: (2b|!2b)==? | last post by:
I have the following line in my code, which is supposed to skip commas and white space and to read a comma seperated value string into the appropriate variables: sscanf(temp.c_str(),...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.