473,699 Members | 2,219 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double printf format...

in program below

#include <stdio.h>
int main()
{
int id = 123, io = 0123, ih = 0x123;
long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
float f = 123.456f;
double d = 01234.56789L;

printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
id, id, id, io, io, io, ih, ih, ih);
printf("ld = %ld\t%lo\t%lx\n lo = %ld\t%lo\t%lx\n lh = %ld\t%lo\t%l\n" ,
ld, ld, ld, lo, lo, lo, lh, lh, lh);
printf("f = %lf\t%le\t%lg\n d = %lf\t%lE\t%lG\n ", f, f, f, d, d, d);
return 0;
}

the output got is:
id = 123 173 7b
io = 83 123 53
ih = 291 %O 123
ld = 1234567 4553207 12d687
lo = 342391 1234567 53977
lh = 19088743 110642547 1234567
f = 123.456001 1.234560e+02 123.456
d = 1234.567890 1.234568E+03 1234.57

why is there a %O in line 3 instead of octal o/p expected?
and in line for d the last o/p is 1234.57. but %g is either %lf or %le
right? so if it is %lf then why is it 1234.57 instead of 1234.56789?

thanks
Mar 13 '08 #1
12 3553
naunetr wrote:
in program below

#include <stdio.h>
int main()
{
int id = 123, io = 0123, ih = 0x123;
long ld = 1234567L, lo = 01234567l, lh = 0X1234567L;
float f = 123.456f;
double d = 01234.56789L;

printf("id = %d\t%o\t%x\nio = %d\t%o\t%x\nih = %d\t%O\t%X\n",
id, id, id, io, io, io, ih, ih, ih);
printf("ld = %ld\t%lo\t%lx\n lo = %ld\t%lo\t%lx\n lh =
%ld\t%lo\t%l\n" ,
ld, ld, ld, lo, lo, lo, lh, lh, lh);
printf("f = %lf\t%le\t%lg\n d = %lf\t%lE\t%lG\n ", f, f, f, d, d,
d); return 0;
}

the output got is:
id = 123 173 7b
io = 83 123 53
ih = 291 %O 123
ld = 1234567 4553207 12d687
lo = 342391 1234567 53977
lh = 19088743 110642547 1234567
f = 123.456001 1.234560e+02 123.456
d = 1234.567890 1.234568E+03 1234.57

why is there a %O in line 3 instead of octal o/p expected?
The format specifier for octal output is %o, not %O. In C case is
important.
and in line for d the last o/p is 1234.57. but %g is either %lf or %le
right? so if it is %lf then why is it 1234.57 instead of 1234.56789?
I think that's a precision issue.

Mar 13 '08 #2
naunetr wrote:
[...]
why is there a %O in line 3 instead of octal o/p expected?
Because you put it there. The relevant fragment of the format string is
ih = %d\t%O\t%X\n

Notice the %O in the string above? There is no %O conversion specifier,
and the "octal" conversion specifier is %o

thanks Lew. is there a specifier which will put a 0 in front off the octal
number? like 0x for %#x?
What about "0%o" and "0x%x"?

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Mar 13 '08 #3
In article <fr**********@r egistered.motza rella.org>,
naunetr <wi************ @roadtonowhere. invalidwrote:
>thanks Lew. is there a specifier which will put a 0 in front off the octal
number? like 0x for %#x?
%#o

C89 4.9.6.1 The fprintf Function

# The result is to be converted to an "alternate form." For o
conversion, it increases the precision to force the first digit
of the result to be a zero. For x (or X) conversion, a
nonzero result will have 0x (or 0X) prefixed to it. [...]

>sorry got it confused with scanf! but why is there a %F too? shouldn't %f
be enough?
C89 does not have %F (but does have %E and %G). %E and %G use a
capital E for the exponent signifier instead of a lower-case e
but are otherwise the same as %e and %g respectively.
--
"The quirks and arbitrariness we observe force us to the
conclusion that ours is not the only universe." -- Walter Kistler
Mar 13 '08 #4
On Thu, 13 Mar 2008 19:16:27 +0000, ro******@ibd.nr c-cnrc.gc.ca (Walter
Roberson) wrote:

On Thu, 13 Mar 2008 19:16:27 +0000, ro******@ibd.nr c-cnrc.gc.ca (Walter
Roberson) wrote:
In article <fr**********@r egistered.motza rella.org>, naunetr
<wi************ @roadtonowhere. invalidwrote:
>>thanks Lew. is there a specifier which will put a 0 in front off the
octal number? like 0x for %#x?

%#o
oops! thought that works only for hex specifier %x.
C89 4.9.6.1 The fprintf Function

# The result is to be converted to an "alternate form." For o
conversion, it increases the precision to force the first digit of
the result to be a zero. For x (or X) conversion, a nonzero result
will have 0x (or 0X) prefixed to it. [...]

>>sorry got it confused with scanf! but why is there a %F too? shouldn't
%f be enough?

C89 does not have %F (but does have %E and %G). %E and %G use a capital
E for the exponent signifier instead of a lower-case e but are otherwise
the same as %e and %g respectively.
thanks Walter. by "C89" you mean ANSI C yes?

Mar 14 '08 #5
naunetr said:
On Thu, 13 Mar 2008 19:16:27 +0000, ro******@ibd.nr c-cnrc.gc.ca (Walter
Roberson) wrote:
<snip>
>C89 does not have %F (but does have %E and %G). %E and %G use a capital
E for the exponent signifier instead of a lower-case e but are otherwise
the same as %e and %g respectively.

thanks Walter. by "C89" you mean ANSI C yes?
Almost certainly it's the other way around - that is, by "ANSI C" you mean
C89.

Quick history for you:

dawnatime: R invents C.
dawnatime + a bit: K says "that's cool, let's do a book on it".
1978: K&R published.
1980s: ANSI undertake a standardisation process which culminates in:
1988: K&R2 (based on draft-proposed ANSI C) published.
1989: ANSI formally standardises C (this is C89, which is what you've got)
1990: ISO adopts the ANSI C Standard internationally (C90)
1999: ISO formally re-standardises C (C99)
2000: ANSI adopts C99
1999-2008: Almost the entire universe ignores C99 completely.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 14 '08 #6
Richard Heathfield <rj*@see.sig.in validwrites:
Quick history for you:
<snip history>
1999-2008: Almost the entire universe ignores C99 completely.
This reflects your personal opinion more accurately than it describes
the truth. It *is* true, of course, but uninterestingly true since
"almost the entire universe ignores X completely" is true for a very
large set of Xs -- including C90!

--
Ben.
Mar 14 '08 #7
Ben Bacarisse said:
Richard Heathfield <rj*@see.sig.in validwrites:
>Quick history for you:
<snip history>
>1999-2008: Almost the entire universe ignores C99 completely.

This reflects your personal opinion more accurately than it describes
the truth.
Well, actually it reflects my personal understanding of the truth. The only
validated C99 compilers I know about are:

EDG (Pentium PIII, Linux 7.x series)
IBM XL (IBM POWER PC_970, AIX 5.3h)
IBM Visual Age (IBM PowerPC systems w/ CHRP system architecture, AIX 5L for
POWER, Version 5.2)
LMPCC C99 Compiler for Linux / PowerPC (Motorola PowerPC MPC7455, Terrasoft
Solutions, Yellow Dog Linux 3.0)
Sun Studio 9 (SPARC Ultra/Intel x86/AMD 64, Solaris 10)

(Some of these depend on the validated C99-conforming Dinkum Unabridged
Library.)

Data source: http://www.peren.com/pages/cvsa_isocvpl.htm
I don't have time to research into how many C90-conforming compilers there
are, but ISTR that there are a hundred or so. Does anyone have any better
data?
--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 14 '08 #8
In article <EO************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>>1999-2008: Almost the entire universe ignores C99 completely.
>This reflects your personal opinion more accurately than it describes
the truth.
>Well, actually it reflects my personal understanding of the truth. The only
validated C99 compilers I know about are:
The absence of validated C99 compilers doesn't mean that the universe
is completely ignoring C99. For example, I can use inline functions
or VLAs with fair confidence that they will be available, and
sufficiently compatible, on all the systems I care about. I don't
think this would be true without the existence of C99.

-- Richard

--
:wq
Mar 14 '08 #9
Richard Tobin said:

<snip>
The absence of validated C99 compilers doesn't mean that the universe
is completely ignoring C99. For example, I can use inline functions
or VLAs with fair confidence that they will be available, and
sufficiently compatible, on all the systems I care about. I don't
think this would be true without the existence of C99.
Understood, but the difficulty is in identifying which features of C99 can
be used "portably". We sure could use a central reference, listing C99
features down the left, compilers across the top, and ticks in the boxes
where the feature has been implemented according to C99 semantics. That
way, maybe people could start to use C99 features with some degree of
confidence that they will be portable to "all the systems I care about",
for various values of "I".

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Mar 14 '08 #10

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

Similar topics

6
30160
by: J | last post by:
Would anyone know if there a type tag to format a double? I have f for floating point, but cannot find one for double.
5
22960
by: Piotr B. | last post by:
Hello, I use MingGW g++ 3.2.3 on Windows 2000/AMD Athlon XP. I tried to output a "long double" variable using stdio printf(). I've tried various %formats (%llf, %Lf etc.), but none of them worked... The same source compiled with Borland C++Builder 5.0 works for %Lf format. How to get the same results with MinGW g++ ?
3
3221
by: Hans Ginzel | last post by:
Hello, let us consider function int foo(char *name, void *data) { ... printf(name, data); /* Should be *data? */ ... }
10
18761
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
67
9894
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the quesion. I have begun to familiarize myself here with the gcc compiler in a win32 environment, in the form of MinGW using both Dev C++ and MSYS as interfaces. I have recompiled some old math code that uses long double types throughout and...
17
2503
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
25
1883
by: Digital Puer | last post by:
I'm getting a very weird bit corruption in a double. I am on an Intel Red Hat Linux box. uname -a returns: Linux foo.com 2.6.9-34.0.2.ELsmp #1 SMP Fri Jun 30 10:33:58 EDT 2006 i686 i686 i386 GNU/Linux I have a "double" variable that is set to 0.00. Some number crunching then occurs, and later on, when I printf this variable with printf("%f"), I am getting 0.00000. However, when I compare
9
1838
by: nsa.usa | last post by:
Hi, Converting a double to int by doing h=(int)(j)in the below code doesn't give the expected result. I'm trying to create 32 random numbers from 1 to 36. So as you see in the output, I get f.ex. in the first line j=10.000000 but converted to int is equals h=1103175862 ???? what gives? please help, I have a headache now, I read the faq it doesn't help :-( Cheers and thanks!, Tobias
6
16734
by: RoS | last post by:
the format for conversion of double in the function printf is "%f" or "%lf" or both? for example printf("%f", (double)0.0); printf("%lf", (double)0.0); are both ok? Good morning Thank you
6
9218
by: amarapreet | last post by:
hi gcc 3.4 says in a warning that %lf is not recognized as a scanf format specifier is this right if so how to read in a double using scanf thanks
0
8691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8620
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
9180
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
9038
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
8920
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
5877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3060
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2351
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.