input values with scanf

I

Intaek LIM

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. :)
 
B

Bigdakine

Subject: input values with scanf
From: (e-mail address removed) (Intaek LIM)
Date: 8/1/03 5:29 AM Hawaiian Standard Time
Message-id: <[email protected]>

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"
 
B

Barry Schwarz

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

Dan Pop

In said:
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++

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
 
Z

Zoran Cutura

Intaek LIM said:
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)".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top