473,326 Members | 2,126 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,326 software developers and data experts.

input values with scanf

Hello.

I always used cin in C++ for standard input.
But, with C, I must use scanf.
Integers, chars are all fine with scanf.

The problem is... I cannot read floating point value like 'double' with scanf.

-----------------------
double d;
printf("Input a value : ");
scanf("%f", &d); // it does not works!
printf("%f", d); // result is 0.000000 in Dev-Cpp, garbage value in VC++
-----------------------

But when I changed %f with %lf, it worked!.
I do not know why. Please let me know.
(The fact throw me into confusion is, being with '%f' works with printf.)

ps. My english is not perfect because it is not my mother tongue. :-)
Nov 13 '05 #1
4 7955
>Subject: input values with scanf
From: li******@nownuri.net (Intaek LIM)
Date: 8/1/03 5:29 AM Hawaiian Standard Time
Message-id: <d9**************************@posting.google.com >

Hello.

I always used cin in C++ for standard input.
But, with C, I must use scanf.
Integers, chars are all fine with scanf.

The problem is... I cannot read floating point value like 'double' with
scanf.

-----------------------
double d;
printf("Input a value : ");
scanf("%f", &d); // it does not works!
printf("%f", d); // result is 0.000000 in Dev-Cpp, garbage value in VC++
-----------------------

But when I changed %f with %lf, it worked!.
I do not know why. Please let me know.
(The fact throw me into confusion is, being with '%f' works with printf.)


You got lucky. %lf should be used as the format specifier with either printf or
scanf.

Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up
requires a creationist"
Nov 13 '05 #2
On 02 Aug 2003 22:15:30 GMT, bi*******@aol.comGetaGrip (Bigdakine)
wrote:
Subject: input values with scanf
From: li******@nownuri.net (Intaek LIM)
Date: 8/1/03 5:29 AM Hawaiian Standard Time
Message-id: <d9**************************@posting.google.com >

Hello.

I always used cin in C++ for standard input.
But, with C, I must use scanf.
Integers, chars are all fine with scanf.

The problem is... I cannot read floating point value like 'double' with
scanf.

-----------------------
double d;
printf("Input a value : ");
scanf("%f", &d); // it does not works!
printf("%f", d); // result is 0.000000 in Dev-Cpp, garbage value in VC++
-----------------------

But when I changed %f with %lf, it worked!.
I do not know why. Please let me know.
(The fact throw me into confusion is, being with '%f' works with printf.)


You got lucky. %lf should be used as the format specifier with either printf or
scanf.

Since both floats and doubles must be passed to printf (or any other
variadic function) as doubles, there is no language reason why %f will
not work with either type of variable. Luck has nothing to do with
it.
<<Remove the del for email>>
Nov 13 '05 #3
In <d9**************************@posting.google.com > li******@nownuri.net (Intaek LIM) writes:
The problem is... I cannot read floating point value like 'double' with scanf.

-----------------------
double d;
printf("Input a value : ");
scanf("%f", &d); // it does not works!
printf("%f", d); // result is 0.000000 in Dev-Cpp, garbage value in VC++
-----------------------

But when I changed %f with %lf, it worked!.
I do not know why. Please let me know.
Did you consider opening your favourite C book and reading the
specification of the scanf function?
(The fact throw me into confusion is, being with '%f' works with printf.)


There is nothing confusing here, considering that printf expects values
and *not* pointers. Clue: is it possible to pass a float value to a
variadic function?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
Intaek LIM <li******@nownuri.net> wrote:
Hello.

I always used cin in C++ for standard input.
But, with C, I must use scanf.
Integers, chars are all fine with scanf.

The problem is... I cannot read floating point value like 'double' with scanf.

-----------------------
double d;
printf("Input a value : ");
scanf("%f", &d); // it does not works!
printf("%f", d); // result is 0.000000 in Dev-Cpp, garbage value in VC++
-----------------------

But when I changed %f with %lf, it worked!.
I do not know why. Please let me know.
(The fact throw me into confusion is, being with '%f' works with printf.)

ps. My english is not perfect because it is not my mother tongue. :-)


In fact many seem to imply that format specifiers of the printf family
and the scanf family of functions do the same, but how could they? While
printf is supposed to produce formatted output scanf is supposed to read
formatted input. It should be clear that the two require different
arguments.

Wherever possible, format specifiers seem to have been choosen to be as
similar as possible in both scanf and printf, but they usually require
different arguments.

As to what the problem with double/float is with format specifiers,
float is just plain simply passed as double when it is passed through a
not prototyped function or part of function. That is with printf being a
so called variadic function, even if you where passing a float value to
printf it would get passed as double and printf would have to access it
as double. So there is no need for printf to distinguish the two.
But scanf, doesn't get a float or double, it gets pointer to float or
pointer to double, which are not magically converted to something other,
so this requires that scanf needs to know which is being passed and this
is achieved by using the l-modifier for the %f format specifier when
passing double.

BTW, saying "I don't know why" is like saying "I'm to lazy to look it
up". You probably meant "I don't understand why, even though I read the
documentation (or FAQ, or else)".

--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 13 '05 #5

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

Similar topics

3
by: Antoni | last post by:
Hello, I wondered if anyone could advise? I am trying to write a basic script, which allows user's to enter there username, password in text fields. Then we take these values and pass them to...
11
by: TechNovice | last post by:
Hi: I'm trying to find a way to test input values. To test an integer I tried this code: ******Code****** int input_number; cin>>input_number; while(!input_number) cout<<"invalid...
2
by: jackson2005 | last post by:
OK, I need to do three different things. On the ONLOAD event I would like a popup box to open. In this popup box I need two text boxes. One for the UserName and one for the BillingTo name. ...
7
by: shocron | last post by:
problem: input values not recognized in dinamicly loaded IFRAMEs here is the thing I have a parent window that has an IFRAME I then load a diffrent page into the IFRAME that contains an input ...
1
by: Ray Z | last post by:
I would like to have someone to help me out with my program. Users are able to add items if they wish for the Monthly Claims Charges System. I did not manage to do the calculation part of the...
2
by: mypublicmail | last post by:
I'm moving big chunks of html into and out of divs using innerHTML. Or, I thought I was until I tested it on Firefox 1.5. Firefox will move the html just fine, but if you have changed any input...
3
by: gregpinero | last post by:
Would someone mind tell me what I am doing wrong here? I want to find all of the input elements within a div tag with id newID and blank out the values in the inputs. Nothing seems to happen when...
6
by: DLP35 | last post by:
I'm sure this should be simple enough but I'm really struggling to get my mind around this. I don't have access to a server but am developing a site and I need to pass information from input boxes...
21
by: dadimar | last post by:
I'm trying to write a program that reads unspecified number of positive and negative values, counts them and computes the average of the input values, not counting zeros. The program should end with...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.