473,663 Members | 2,876 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2459
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

"siliconwaf er" <sp*********@ya hoo.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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 , "siliconwaf er"
<sp*********@ya hoo.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*****@earthl ink.net> wrote in message
news:gE******** *********@newsr ead2.news.atl.e arthlink.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
2292
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 end of this post. Please read the email before you respond to it. Generally, the struct module is for packing and unpacking of binary data. It includes support to pack and unpack the c types: byte, char, short, long, long long, char, *, and...
2
10685
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
5287
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 specification for each column (e.g. printf(%10s %25s %15s\n", pszCol1, pszCol2, pszCol3)). My problem is that when a string is longer the column's width, it overflows the column and takes the table out of alignment. What I want it to do is word-wrap...
11
5933
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 <= 4 */ };
1
4215
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.. but I've never seen a detailed descriptions of all that's possible. For example, Dim n As Integer = 65536
6
7495
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" ? (adding +/- sign) "% 3d" ? (adding a space in case of positive vaklue) (E.g - for zero padding - "%03d" is {0:d3}, but how do I do space
3
12791
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 scanf("%d%d",...) different from scanf("%d %d",...) in the Standard's point of view? Thank you.
2
5706
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 know any document about these, and any other
1
4196
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(), "%f%*,%d%*,%f%*,%d%*,%d%*,%f%*,%f%*,%f%*,%f%*,%d%*,%f%*,%d%*,%d", &dt, &ti, &lsp, &lst, &lsv, &o, &h, &l, &bd, &bv, &ak, &av, &cv) ; I suspect the format specifiers are incorrect, since I am not reading
0
8345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8771
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8548
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8634
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6186
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
2000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1757
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.