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


Groups > comp.lang.c > #6963

Re: printf & scanf order

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: printf & scanf order
Date 2011-06-26 08:27 -0400
Organization A noiseless patient Spider
Message-ID <iu78ls$3uj$1@dont-email.me> (permalink)
References <88abc4aa-ecdb-4136-98a8-112a78916d6e@28g2000pry.googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web