Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #6954 > unrolled thread

printf & scanf order

Started byflebber <flebber.crue@gmail.com>
First post2011-06-25 22:09 -0700
Last post2011-06-26 17:17 +0200
Articles 9 — 6 participants

Back to article view | Back to comp.lang.c


Contents

  printf & scanf order flebber <flebber.crue@gmail.com> - 2011-06-25 22:09 -0700
    Re: printf & scanf order pete <pfiland@mindspring.com> - 2011-06-26 02:06 -0400
      Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 14:58 +0200
        Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 15:16 +0200
          Re: printf & scanf order Ralf Damaschke <rwspam@gmx.de> - 2011-06-26 14:12 +0000
          Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 16:20 +0200
          Re: printf & scanf order "J. J. Farrell" <jjf@bcs.org.uk> - 2011-06-28 03:59 +0100
    Re: printf & scanf order Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-26 08:27 -0400
    Re: printf & scanf order "Heinrich Wolf" <invalid@invalid.invalid> - 2011-06-26 17:17 +0200

#6954 — printf & scanf order

Fromflebber <flebber.crue@gmail.com>
Date2011-06-25 22:09 -0700
Subjectprintf & scanf order
Message-ID<88abc4aa-ecdb-4136-98a8-112a78916d6e@28g2000pry.googlegroups.com>
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;
}

[toc] | [next] | [standalone]


#6957

Frompete <pfiland@mindspring.com>
Date2011-06-26 02:06 -0400
Message-ID<4E06CC51.5204@mindspring.com>
In reply to#6954
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

[toc] | [prev] | [next] | [standalone]


#6965

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2011-06-26 14:58 +0200
Message-ID<iu7aeb$gjj$1@news-1.m-online.net>
In reply to#6957
"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 

[toc] | [prev] | [next] | [standalone]


#6967

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2011-06-26 15:16 +0200
Message-ID<iu7bf0$h09$1@news-1.m-online.net>
In reply to#6965
"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

[toc] | [prev] | [next] | [standalone]


#6968

FromRalf Damaschke <rwspam@gmx.de>
Date2011-06-26 14:12 +0000
Message-ID<Xns9F10A4D7438B9dammel@y2plugh.fqdn.th-h.de>
In reply to#6967
"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

[toc] | [prev] | [next] | [standalone]


#6969

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2011-06-26 16:20 +0200
Message-ID<iu7f7k$in6$1@news-1.m-online.net>
In reply to#6967
"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". 

[toc] | [prev] | [next] | [standalone]


#7118

From"J. J. Farrell" <jjf@bcs.org.uk>
Date2011-06-28 03:59 +0100
Message-ID<iubg1p$meo$1@dont-email.me>
In reply to#6967
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.

[toc] | [prev] | [next] | [standalone]


#6963

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-06-26 08:27 -0400
Message-ID<iu78ls$3uj$1@dont-email.me>
In reply to#6954
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-faq.com/>.

> 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

[toc] | [prev] | [next] | [standalone]


#6970

From"Heinrich Wolf" <invalid@invalid.invalid>
Date2011-06-26 17:17 +0200
Message-ID<iu7iim$k5r$1@news-1.m-online.net>
In reply to#6954
"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;
  }

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web