473,397 Members | 1,974 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,397 software developers and data experts.

Variable of type DOUBLE converts number strangely.

I'm new to C and I know this is probably one of those "most commonly
mis-interpreted as a problem", newcomer type of question, but for this code:
double x;
printf("Enter number:");
scanf("%f", &x);
printf("The number is %f", x);
If I enter 2 as the number, the following is returned:
-1.997517

This doesn't happen for 'float', only for 'double'.

Is there something I'm not getting here? Is there something I'm
supposed to be doing?

I'm teaching myself with GCC v3.3.6, and while I recall reading
somewhere that floating point numbers do slightly different things, I
didn't think they'd be THAT different!
May 26 '06 #1
6 1678
John wrote:
I'm new to C and I know this is probably one of those "most commonly
mis-interpreted as a problem", newcomer type of question, but for this
code:
double x;
printf("Enter number:");
scanf("%f", &x);
printf("The number is %f", x);
If I enter 2 as the number, the following is returned:
-1.997517

This doesn't happen for 'float', only for 'double'.

Is there something I'm not getting here? Is there something I'm
supposed to be doing?

I'm teaching myself with GCC v3.3.6, and while I recall reading
somewhere that floating point numbers do slightly different things, I
didn't think they'd be THAT different!


The difference is not where you think, but that the specifiers for scanf
and not what they are for printf. When you supply printf with x an
argument, the value of x is promoted to a double, whether x was a float
or a double, so a single specifier ("%f") works for both. But scanf
gets the address of that x supplied as a argument. When x is a float,
&x is the address of a float; when x is a double, &x is the address of a
double. Those are different things, which possibly different sizes and
possibly different internal forms, so need different specifiers. See
code below:

#include <stdio.h>

int main(void)
{
double x;
char input_string[] = "2";

/* similar to your posted code */
printf("Enter number:");
fflush(stdout);
#if 0
scanf("%f", &x); /* replaced so not dependent on
keyboard typing */
#endif
sscanf(input_string, "%f", &x); /* gcc should give a diagnostic if
you invoke it with reasonable
options. */
printf("%s\n", input_string); /* to look like you typed it */
printf("The number is %f\n\n", x);

/* notice the change in the sscanf specifier below */
printf("Enter number:");
fflush(stdout);
sscanf(input_string, "%lf", &x);
printf("%s\n", input_string);
printf("The number is %f\n", x);

return 0;
}


May 26 '06 #2
John wrote:

I'm new to C and I know this is probably one of those "most commonly
mis-interpreted as a problem", newcomer type of question, but for this code:

double x;
printf("Enter number:");
scanf("%f", &x);
printf("The number is %f", x);

If I enter 2 as the number, the following is returned:
-1.997517

This doesn't happen for 'float', only for 'double'.


scanf and printf are not symetric on %f.
%f prints a double.
%f scanfs a float.
%lf scanfs a double.

--
pete
May 26 '06 #3
Martin Ambuhl wrote:
John wrote:
I'm new to C and I know this is probably one of those "most commonly
mis-interpreted as a problem", newcomer type of question, but for this
code:
double x;
printf("Enter number:");
scanf("%f", &x);
printf("The number is %f", x);
If I enter 2 as the number, the following is returned:
-1.997517

This doesn't happen for 'float', only for 'double'.

Is there something I'm not getting here? Is there something I'm
supposed to be doing?

I'm teaching myself with GCC v3.3.6, and while I recall reading
somewhere that floating point numbers do slightly different things, I
didn't think they'd be THAT different!

The difference is not where you think, but that the specifiers for scanf
and not what they are for printf. When you supply printf with x an
argument, the value of x is promoted to a double, whether x was a float
or a double, so a single specifier ("%f") works for both. But scanf
gets the address of that x supplied as a argument. When x is a float,
&x is the address of a float; when x is a double, &x is the address of a
double. Those are different things, which possibly different sizes and
possibly different internal forms, so need different specifiers. See
code below:

#include <stdio.h>

int main(void)
{
double x;
char input_string[] = "2";

/* similar to your posted code */
printf("Enter number:");
fflush(stdout);
#if 0
scanf("%f", &x); /* replaced so not dependent on
keyboard typing */
#endif
sscanf(input_string, "%f", &x); /* gcc should give a diagnostic if
you invoke it with reasonable
options. */
printf("%s\n", input_string); /* to look like you typed it */
printf("The number is %f\n\n", x);

/* notice the change in the sscanf specifier below */
printf("Enter number:");
fflush(stdout);
sscanf(input_string, "%lf", &x);
printf("%s\n", input_string);
printf("The number is %f\n", x);

return 0;
}

Ah, so I see. There is a different data type identifier I should use to
scanf a double, "%lf", which I guess stands for "long float"? The book
I'm studying didn't get to that and all previous examples so far have
been "%f".

Anyway, when I add that to my scanf, everything works fine now. THanks!
May 26 '06 #4
pete <pf*****@mindspring.com> writes:
[...]
scanf and printf are not symetric on %f.
%f prints a double.
%f scanfs a float.
%lf scanfs a double.


To expand on that:

%f prints a double, but in effect it can print either a float or a
double (because a float is impicitly promoted to double).

No such promotion can occur for scanf, which expects a pointer to a
float or double *object*, not just a float or double *value*.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
May 26 '06 #5
Martin Ambuhl wrote:
John wrote:
I'm new to C and I know this is probably one of those "most commonly
mis-interpreted as a problem", newcomer type of question, but for this
code:
double x;
printf("Enter number:");
scanf("%f", &x);
printf("The number is %f", x);
If I enter 2 as the number, the following is returned:
-1.997517
I'm teaching myself with GCC v3.3.6>
The difference is not where you think, but that the specifiers for scanf
and not what they are for printf.


gcc with -Wall catches this easily.

May 27 '06 #6
Martin Ambuhl wrote:
John wrote:
I'm new to C and I know this is probably one of those "most commonly
mis-interpreted as a problem", newcomer type of question, but for this
code:
double x;
printf("Enter number:");
scanf("%f", &x);
printf("The number is %f", x);
If I enter 2 as the number, the following is returned:
-1.997517
I'm teaching myself with GCC v3.3.6>
The difference is not where you think, but that the specifiers for scanf
and not what they are for printf.


gcc with -Wall catches this easily.

HTH
Regards,
Frodo Baggins

May 27 '06 #7

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

Similar topics

2
by: Suzanne Vogel | last post by:
'stdarg.h' defines the 'va_arg' type to use in passing variable numbers of parameters to functions. The example of its use given in the Dinkumware documentation *seems* to imply that the 'va_arg'...
4
by: cindy liu | last post by:
Hi, In .Net, how to convert a string to a double? Thanks in advance! Cindy
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
4
by: dc15 | last post by:
For an intro to VB project I have to write a program which takes an amount of Miles, Yards, and Inches.....and converts it to metric (KM, M, and CM) when all values are entered to the input text...
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
5
by: John | last post by:
Which variable type (c#) can whole the largest whole number? I know this sounds silly but as double and decimal are made for numbers with decimals I am not sure. Also if anybody knows of any...
6
by: CptDondo | last post by:
How do you declare a function with 0 or mroe arguments? I have a bunch of functions like this: void tc_cm(int row, int col); void tc_do(void); void tc_DO(int ln); and I am trying to ...
4
by: Jim Michaels | last post by:
how do I write an overloaded >operator for istream that let's say fraction class through set() can take several types: void set(char*) void set(long int num, long int den) void set(double) ...
3
by: d.avitabile | last post by:
I have a C++ code that is reading a list of parameters from a file. PARAMETERS stringParam="stringValue", intParam=4, doubleParam = 3.533, ... END The values can be strings as well as integers...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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,...
0
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...

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.