Re: Extra blank lines from printf.

Discussion in 'C Programming' started by bartc, Mar 24, 2010.

  1. bartc

    bartc Guest

    "Robbie Hatley" <> wrote in message
    news:...
    >
    > Perhaps I'm making some newbie, bonehead mistake here, but
    > I can't see what it is. I wrote a simple program which
    > writes a table of celsius-to-fahrenheit conversions.
    > I've included the entire program at the bottom of this post
    > for reference, but the guts of the program are the following
    > lines:
    >
    > printf(" Fahr Cels\n");
    >
    > for (Fahr = FahrMin; Fahr <= FahrMax; Fahr += FahrInc)
    > {
    > Cels = (((Fahr-32)*100.0)/180.0);
    > printf("%10.3f %10.3f\n", Fahr, Cels);
    > }
    >
    > When I run "CelsFahr 40 41 0.2, I expected it to print:
    >
    > Cels Fahr
    > 40.000 104.000
    > 40.200 104.360
    > 40.400 104.720
    > 40.600 105.080
    > 40.800 105.440
    >
    > but what it *actually* prints is:
    >
    > Cels Fahr
    > 40.000 104.000
    >
    > 40.200 104.360
    >
    > 40.400 104.720
    >
    > 40.600 105.080
    >
    > 40.800 105.440
    >
    > Why the extra blank lines? printf() doesn't automatically
    > print a newline, does it? Don't you have to put those in
    > manually? So I don't see why I'm getting extra blank lines.
    > Am I missing something obvious?


    What happens when you leave out the "\n"?

    What about just doing printf ("abc\ndef\n"); ?

    Or puts("abc"); puts("def"); ?

    (I'm assuming the display width is more than about 24 characters.)
    --
    Bartc
     
    bartc, Mar 24, 2010
    #1
    1. Advertising

  2. bartc

    bartc Guest

    "Robbie Hatley" <> wrote in message
    news:...
    >
    > "bartc" wrote:
    >
    >> What happens when you leave out the "\n"?
    >>
    >> What about just doing printf ("abc\ndef\n"); ?
    >>
    >> Or puts("abc"); puts("def"); ?


    > Ok, I'll test those. Here's a simple program:


    > printf("abcd");
    > printf("efgh");
    > printf("ijkl");

    ....
    > OUTPUT:
    >
    > abcdefghijkl
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > abc
    > def
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    > abc
    > def
    >
    > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    >
    > printf() isn't automatically inserting newlines.
    > I didn't think it would, as other programs I've written
    > and compiled on same compiler don't put them in.


    OK, so printf() works as it should. So now, starting either with a simple,
    working printf and adding to it, or the other way, try and see where the
    problem creeps in.

    These are suggestions (or perhaps put all of them in the loop; after which
    one is there an extra newline?):

    printf("<>\n");
    printf("<%d>\n", (int)Fahr);
    printf("<%10d>\n", (int)Fahr);
    printf("<%10.3f>\n", Fahr);
    printf("<%10.3f %10.3f>\n", Fahr, Cels);

    Then that might give a clue as to what's causing the problem (as I can't
    recreate it). Maybe a hidden control character in the format string (I
    didn't notice one)?

    --
    Bartc
     
    bartc, Mar 24, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andy Fish
    Replies:
    1
    Views:
    1,506
    Andy Fish
    Dec 21, 2004
  2. Eric Sosman

    Re: Extra blank lines from printf.

    Eric Sosman, Mar 24, 2010, in forum: C Programming
    Replies:
    1
    Views:
    352
    Eric Sosman
    Mar 24, 2010
  3. David Ainley
    Replies:
    3
    Views:
    193
    Jesús Gabriel y Galán
    Aug 4, 2010
  4. Cah Sableng
    Replies:
    0
    Views:
    261
    Cah Sableng
    Apr 23, 2007
  5. OccasionalFlyer

    Chrome Inserts Extra Blank Lines in Drop-Down list

    OccasionalFlyer, Apr 22, 2010, in forum: Javascript
    Replies:
    1
    Views:
    210
Loading...

Share This Page