473,799 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

incorrect printf o/p for doubles

Hi,

I am getting incorrect o/p from the code below:

#include<stdio. h>
#include<float. h>
#include<limits .h>
main()
{
double val,val1;
val=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;
printf("%f\n",v al2);
}

The result I get is 152415787531839 67100000.000000 instead of the correct value.

I am running on HP-UX 11 with ansi c compiler.

Can anyone let me know why I am getting this result.
Nov 13 '05
13 3916
Keith Thompson <ks*@cts.com> wrote in message news:<lz******* *****@cts.com>. ..
James Hu <jx*@despammed. com> writes:
On 2003-10-30, Yogesh Khanolkar <yo************ **@azuresolutio ns.com> wrote:

{
double val,val1;
val=12345678901 2.123456;
val1=1234567890 12.123456;
val2=val*val1;


It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.


Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


Crikes! Thanks for the correction.

-- James
Nov 13 '05 #11
On Fri, 31 Oct 2003, Keith Thompson wrote:
Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


Never in standard C at least, but where does this odd interpretation
come from then? Did K&R allow it or is it just some awfully widespread
hack?

Nov 13 '05 #12
On 2003-11-01, Jarno A Wuolijoki <jw******@cs.He lsinki.FI> wrote:
On Fri, 31 Oct 2003, Keith Thompson wrote:
Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


Never in standard C at least, but where does this odd interpretation
come from then? Did K&R allow it or is it just some awfully widespread
hack?


Many pre-ANSI C compilers supported a syntax along the lines of:

var;

to declare a global integer variable. It also supported a syntax of

foo(var)
var; /* this line is optional */
{
/* ... */
}

to declare an integer parameter. (As an aside, gcc still accepts
the above syntax.)

I incorrectly assumed pre-ANSI C compilers allowed the implicit
declaration of an integer variable, similar to the way pre-ANSI C
and C90 do for a function returning int.

-- James
Nov 13 '05 #13
In <lz************ @cts.com> Keith Thompson <ks*@cts.com> writes:
James Hu <jx*@despammed. com> writes:
On 2003-10-30, Yogesh Khanolkar <yo************ **@azuresolutio ns.com> wrote:
>
> #include<stdio. h>
> #include<float. h>
> #include<limits .h>
> main()
> {
> double val,val1;
> val=12345678901 2.123456;
> val1=1234567890 12.123456;
> val2=val*val1;
> printf("%f\n",v al2);
> }
>


It looks like val2 is defaulting to an int type, and you are trying to
print it out like a double.


Undeclared variables don't default to int. Some compilers, especially
old ones, might let you get away with

val2;

as a declaration for val2 with a default type of int, but it's never
been possible to omit the declaration altogether.


And that is/was allowed at file scope only, because at block scope this
looks like an expression statement with an undeclared identifier.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #14

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

Similar topics

5
3100
by: dustu | last post by:
Hi, I'm using lcc-win32 on Windows 2000 as compiler/IDE. The details are: Input file (sample - the no. of digits per line may change): ---> 1111111111111111.00 1111111111111111.01 1111111111111111.02 <---
9
2915
by: | last post by:
void show( char *s, ...) is a function seemd like prinf code -------------- #include <stdio.h> #include <stdarg.h> void show( char *s, ...) { va_list stage;
2
1976
by: David Mathog | last post by:
Is there some method for restricting the maximum number of characters that will be emitted by a printf() function? Where printf() here is generic and refers to sprintf, fprintf, etc. Here's an example of where this would be used (partial code!) double thevar; char abuf; int oc;
69
5604
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
12
43472
by: Zero | last post by:
Hi everybody, i want to write a small program, which shows me the biggest and smallest number in dependance of the data type. For int the command could be: printf("\n%20s\t%7u\t%13i\t%13i","signed int",sizeof(signed int),INT_MIN,INT_MAX);
34
16002
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
1
2405
by: ndawg123 | last post by:
Hey guys what im trying to do is write a yatzee game with C. And im stuck already and its the start?!?! I want the user to type there 5 numbers. i.e My program so far does this Please enter dice Values:
11
2681
by: vectorizor | last post by:
Hi guys, My program deals with with lots of large matrices. To easily debug it, I would like to be able to dump them in files, so that I can easily import then in other software (think matlab, excel) so I can analyse the intermediate results. To make sure the data can be understood by other programs, a space need to be included between each cell, and a return at the end of each row (row-major order). Hence at the moment, the dump code...
29
11125
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
9544
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
10259
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
10238
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
10030
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
9077
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...
0
6809
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();...
1
4145
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
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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.