printf & scanf order

F

flebber

Hi

I am starting to learn c and have been playing with several simple
scripts to learn.

Question why does the request for scanf precede printf. I created a
simple script to calculate interest. When running the script though
scanf is processed first and the request for input comes first,
despite being in the second position. How do I get printf to call
first.

This is my script.

# include <stdio.h>
int main()
{
float a,b,c,interest;
printf("Type in amount in dollars cents: \n");
scanf("%f",&a);
b = 0.075;
c = a * b;
interest = a + c;
printf("The total including interest is $%.2f", interest);
return 0;
}
 
E

Eric Sosman

Hi

I am starting to learn c and have been playing with several simple
scripts to learn.

Question why does the request for scanf precede printf. I created a
simple script to calculate interest. When running the script though
scanf is processed first and the request for input comes first,
despite being in the second position. How do I get printf to call
first.

This is Question 12.4 on the comp.lang.c Frequently Asked
Questions (FAQ) page at said:
This is my script.

Usually called a "program."
# include<stdio.h>
int main()
{
float a,b,c,interest;
printf("Type in amount in dollars cents: \n");
scanf("%f",&a);

Almost always, when your program receives input from an external
source it will need to check that the input is in the expected form.
Here, you're expecting the user to enter a floating-point number in
the usual way floating-point numbers are written -- but what if the
person actually enters

$19.95

(note the dollar sign), or any of a huge number of other possibilities,
either outright misunderstandings or simple typos? What I'm getting
at is that scanf() tells you how many input items it successfully
converted, and your program should check that the count is what was
expected. See Questions 12.19 and 12.20 in the FAQ.
b = 0.075;
c = a * b;
interest = a + c;
printf("The total including interest is $%.2f", interest);

For safety's sake, make sure the last line of output ends with
a '\n' newline character. (The important thing is that *every*
line should end with '\n', which will be vacuously true if and only
if the final line does.) A line that hasn't been terminated may or
may not ever make it to the eventual output destination; even if it
does, it's likely to get mixed up with things like interactive
prompts.
 
H

Heinrich Wolf

....
# include <stdio.h>

int main(void)
{
float a, b, c, interest;
printf("Type in amount in dollars cents: \n");
fflush(stdout);
scanf("%f",&a);
b = 0.075f;
c = a * b;
interest = a + c;
printf("The total including interest is $%.2f\n", interest);
return 0;
}

(double) is a more natural type than (float).
(double) is not subject to default argument promotions.

# include <stdio.h>

int main(void)
{
double a, b, c, interest;
printf("Type in amount in dollars cents: \n");
fflush(stdout);
scanf("%lf",&a);
b = 0.075;
c = a * b;
interest = a + c;
printf("The total including interest is $%.2f\n", interest);

Hi,

here you forgot to replace %f with %lf
printf("The total including interest is $%.2lf\n", interest);
return 0;
}

kind regards
Heiner
 
H

Heinrich Wolf

....
Hi,

here you forgot to replace %f with %lf
printf("The total including interest is $%.2lf\n", interest);

Uuh!

I just read FAQ 12.9 and more.
%f is correct.
%lf is undefined in printf

I regard this a bad design of printf and scanf, as well as
sscanf("123code", "%d%s", )
might return 123 and "ode", because it does not unget the breaking 'c' in
"%d"

I am sorry
Heiner
 
R

Ralf Damaschke

Heinrich Wolf said:
I regard this a bad design of printf and scanf, as well as
sscanf("123code", "%d%s", )

.... with appropriate arguments following the last comma ...
might return 123 and "ode", because it does not unget the
breaking 'c' in "%d"

If that's how your implementation behaves you should ask the
compiler vendor for a correction or a refund.

-- Ralf
 
H

Heinrich Wolf

....
I just read FAQ 12.9 and more.
%f is correct.
%lf is undefined in printf

I regard this a bad design of printf and scanf, as well as
sscanf("123code", "%d%s", )
might return 123 and "ode", because it does not unget the breaking 'c' in
"%d"

I am sorry
Heiner

I tested with Turbo C 2.0, Borland C++ Builder 5, Fedora 14 cc and Fedora 14
gcc:
They all accept %lf in printf and none strips the c in "code".
 
H

Heinrich Wolf

Hi,

I needed to code two additional fgets after scanf in order to make my
program continue as expected.

#include <stdio.h>

int main(void)
{
double d;
char s[2];

printf("please enter double number: ");
if (scanf("%lf", &d) == 1)
{
printf("read %%f %f\n", d);
/* The following line works for my compilers
Turbo C 2.0, Borland C++ Builder 5,
Fedora 14 cc, Fedora 14 /usr/bin/gcc ,
but it is not guaranteed
*/
printf("read %%lf %lf\n", d);
}
else
fgets(s, sizeof(s), stdin);
fgets(s, sizeof(s), stdin);
puts("please press ENTER");
fgets(s, sizeof(s), stdin);
return 0;
}
 
J

J. J. Farrell

Heinrich said:
...


Uuh!

I just read FAQ 12.9 and more.
%f is correct.
%lf is undefined in printf

The FAQ's out of date. %lf is well-defined in C99, the l is ignored. Its
behaviour was undefined before C99.
I regard this a bad design of printf and scanf, as well as
sscanf("123code", "%d%s", )
might return 123 and "ode", because it does not unget the breaking 'c'
in "%d"

Get your money back, your library is badly broken.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top