[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

printf & scanf order

flebber

6/26/2011 5:09:00 AM

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;
}
8 Answers

pete

6/26/2011 6:06:00 AM

0

flebber wrote:
>
> 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;
> }

I'm not sure if I understand your question.
But if I do, then maybe placing
fflush(stdout);
before your scanf call might solve your problem.

Also, it is best to terminate a text stream
with a newline character in your last printf call.

# 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);
return 0;
}

--
pete

Eric Sosman

6/26/2011 12:28:00 PM

0

On 6/26/2011 1:09 AM, flebber wrote:
> 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 <http://www.c-fa....

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

> return 0;
> }

--
Eric Sosman
esosman@ieee-dot-org.invalid

Dave \Crash\ Dummy

6/26/2011 12:59:00 PM

0


"pete" <pfiland@mindspring.com> schrieb im Newsbeitrag
news:4E06CC51.5204@mindspring.com...
....
> # 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;
> }
>
> --
> pete

kind regards
Heiner

Dave \Crash\ Dummy

6/26/2011 1:16:00 PM

0


"Heinrich Wolf" <invalid@invalid.invalid> schrieb im Newsbeitrag
news:iu7aeb$gjj$1@news-1.m-online.net...
....

>> 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);
>

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

Ralf Damaschke

6/26/2011 2:12:00 PM

0

"Heinrich Wolf" <invalid@invalid.invalid> wrote:

> 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

Dave \Crash\ Dummy

6/26/2011 2:21:00 PM

0


"Heinrich Wolf" <invalid@invalid.invalid> schrieb im Newsbeitrag
news:iu7bf0$h09$1@news-1.m-online.net...

....

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

Dave \Crash\ Dummy

6/26/2011 3:18:00 PM

0


"flebber" <flebber.crue@gmail.com> schrieb im Newsbeitrag
news:88abc4aa-ecdb-4136-98a8-112a78916d6e@28g2000pry.googlegroups.com...

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. Farrell

6/28/2011 2:59:00 AM

0

Heinrich Wolf wrote:
>
> "Heinrich Wolf" <invalid@invalid.invalid> schrieb im Newsbeitrag
> news:iu7aeb$gjj$1@news-1.m-online.net...
> ...
>
>>> 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);
>>
>
> 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.