Confused at newline requirement for printf

Discussion in 'C Programming' started by linq936, Sep 21, 2007.

  1. linq936

    linq936 Guest

    Hi,
    I read in many places that the string to be outputted by printf()
    must be ending with newline, for example, it should be

    printf("Hello World.\n");

    instead of

    printf("Hello World.");

    because the latter is "undefined" by standard.

    But I tried the following code:

    #include <stdio.h>

    int main(){
    printf("%d", 15);

    printf("Type in some number:");
    int i;
    scanf("%d", &i);
    printf("\nHey you typed in %d", i);

    return 0;
    }

    You can see that I do not have '\n" in any of the printf, I see what
    I expected:

    $>./a.out
    15Type in some number:9

    Hey you typed in 9$>

    I know this could be because the compiler implements the support
    and also when program finishes the buffer gets flushed out.

    My real question is like this, we see a lot of use cases that
    printf() and scanf() are used together, as in my example, use printf()
    to prompt user to type in something and then use scanf() to read it
    in. It is best not to move the curer to a new line.

    Then how that requirement that printf() should end with newline
    comes to play?
    linq936, Sep 21, 2007
    #1
    1. Advertising

  2. linq936

    Eric Sosman Guest

    linq936 wrote:
    > Hi,
    > I read in many places that the string to be outputted by printf()
    > must be ending with newline, for example, it should be
    >
    > printf("Hello World.\n");
    >
    > instead of
    >
    > printf("Hello World.");
    >
    > because the latter is "undefined" by standard.


    Not exactly. A "line" of text usually ends with a
    newline character, and if you don't supply one the
    implementation might not treat whatever characters you
    have already supplied as a "line." But there is no
    requirement that all the characters of a "line" must
    be output at the same time:

    printf ("Hell");
    x = sqrt(y);
    printf ("o brave new World ");
    x += atan(x, y);
    printf ("that hath such creatures ");
    x *= y;
    printf ("in it!");
    x = 42;
    printf ("\n");

    .... uses five printf() calls to generate one "line" of
    output, and is perfectly all right. Each of the first
    four printf() calls generates part of a "line," and the
    fifth makes it complete. As long as the newline comes
    along "eventually," all is well.

    > But I tried the following code:
    >
    > #include <stdio.h>
    >
    > int main(){
    > printf("%d", 15);
    >
    > printf("Type in some number:");
    > int i;
    > scanf("%d", &i);
    > printf("\nHey you typed in %d", i);
    >
    > return 0;
    > }
    >
    > You can see that I do not have '\n" in any of the printf, I see what
    > I expected:
    >
    > $>./a.out
    > 15Type in some number:9
    >
    > Hey you typed in 9$>
    >
    > I know this could be because the compiler implements the support
    > and also when program finishes the buffer gets flushed out.


    The implementation is trying to be helpful, probably by
    flushing all pending output on all streams whenever you use
    an input function on any stream. Not all implementations will
    be so helpful (indeed, some may be intentionally *un*helpful,
    because excessive output flushing defeats the purpose of
    buffering and may hurt performance).

    Also, it appears that this implementation can tolerate
    "lines" that lack a newline terminator. Not all can do so.
    Some implementations use entirely different methods to divide
    a file into "lines," such as preceding each line with a number
    giving its length. Such implementations cannot even begin to
    output a line until the '\n' has appeared and the length can
    be computed; if the '\n' never shows up it's anybody's guess
    what will happen to the still-buffered data.

    The blank line is probably (*probably*) caused by the
    implementation echoing the newline (or newline-equivalent)
    that ended your input, not by anything your program did on
    its own.

    > My real question is like this, we see a lot of use cases that
    > printf() and scanf() are used together, as in my example, use printf()
    > to prompt user to type in something and then use scanf() to read it
    > in. It is best not to move the curer to a new line.
    >
    > Then how that requirement that printf() should end with newline
    > comes to play?


    Best practice: Make the prompt be a complete line, with
    newline and all. If there's any chance that the output stream
    might be "fully buffered," call fflush() before attempting input.
    This will put the prompt and the response on separate lines,
    but if the system is incapable of handling a partial line that's
    the best you can manage anyhow.

    Second-best practice: Prompt with a newline-less line, and
    call fflush() before reading the response. This stands a fairly
    good chance of forcing the prompt to appear, and a fairly good
    chance of allowing the response to occupy the same line on the
    terminal.

    Third-best practice: Call setvbuf() very early to make the
    prompting stream unbuffered. Then proceed as in the second-
    best scenario, but now you can omit the fflush(). Note that this
    will make *all* I/O on the stream unbuffered; if you're going to
    begin with a few prompts and then write reams of output you may
    not like the performance.

    Fourth-best practice: Proceed as you did in the code you
    showed, and hope for the best.

    Worst practice: Use gets() to read the input.

    --
    Eric Sosman
    lid
    Eric Sosman, Sep 21, 2007
    #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. Samuel

    No newline using printf

    Samuel, Sep 16, 2005, in forum: Python
    Replies:
    8
    Views:
    1,364
    Gary Herron
    Sep 16, 2005
  2. ben
    Replies:
    4
    Views:
    600
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,225
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    423
  5. guru
    Replies:
    8
    Views:
    265
Loading...

Share This Page