advanced printf() and scanf()

Discussion in 'C Programming' started by Gaijinco, Apr 25, 2006.

  1. Gaijinco

    Gaijinco Guest

    I read somewhere that printf and scanf had "advanced features" and they
    point to:

    scanf("%[^\n]",line); // line is a string

    as an example, where scanf() acts like gets()

    I try to look of more of this "advanced features" in documentation but
    couldn't found any. What "advance features" do you of these functions
    (printf and scanf)?

    Thanks a lot!
    Gaijinco, Apr 25, 2006
    #1
    1. Advertising

  2. Gaijinco

    Vladimir Oka Guest

    Gaijinco wrote:
    > I read somewhere that printf and scanf had "advanced features" and they
    > point to:
    >
    > scanf("%[^\n]",line); // line is a string


    This tells `scanf()` to read everything until the newline. Newline is
    left in the input buffer, and may be read by subsequent calls.

    > as an example, where scanf() acts like gets()


    Making anything behave like `gets()` must be EVIL!

    Think of what happens when there's more charaters in the input buffer
    than you allowed for in your string buffer.

    You're better off using `fgets()` and `sscanf()`.

    > I try to look of more of this "advanced features" in documentation but
    > couldn't found any.


    Try the C Standard. You should be able to download the current version
    of C99 plus TCs as N1124.pdf. Google for it.

    > What "advance features" do you of these functions (printf and scanf)?


    In my line of work, exactly none (don't even use the functions, let
    alone their "advanced" features).
    Vladimir Oka, Apr 25, 2006
    #2
    1. Advertising

  3. In article <>,
    Gaijinco <> wrote:

    >I try to look of more of this "advanced features" in documentation but
    >couldn't found any. What "advance features" do you of these functions
    >(printf and scanf)?


    I don't know if you'd call it an "advanced feature", but I have seen
    examples that would have been a lot simpler if someone had known about
    the use of "*" as the precision modifier.

    Suppose that "s" points to an array of characters whose length is
    "len" - a representation of a string that does not use null-termination.
    This can be printed with

    printf("%.*s", len, s);

    but I have seen programs where the author malloced a buffer, copied
    the string, and null-terminated it so that he could use "%s".

    -- Richard
    Richard Tobin, Apr 25, 2006
    #3
  4. In article <>,
    Vladimir Oka <> wrote:

    >Gaijinco wrote:
    >> I read somewhere that printf and scanf had "advanced features" and they
    >> point to:


    >> scanf("%[^\n]",line); // line is a string


    >This tells `scanf()` to read everything until the newline. Newline is
    >left in the input buffer, and may be read by subsequent calls.


    >> as an example, where scanf() acts like gets()


    As the scanf() form leaves the newline in the input stream, but
    the gets() form does not, the two are not equivilent.

    You could use:

    scanf("%[^\n]\n",line);

    but you should be sure you understand the result you would get
    if end of file occurs before you encounter a \n .
    --
    There are some ideas so wrong that only a very intelligent person
    could believe in them. -- George Orwell
    Walter Roberson, Apr 25, 2006
    #4
  5. In article <e2lfg7$h3o$>, -cnrc.gc.ca (Walter Roberson) writes:
    > >Gaijinco wrote:
    > >> as an example, where scanf() acts like gets()

    >
    > As the scanf() form leaves the newline in the input stream, but
    > the gets() form does not, the two are not equivilent.
    >
    > You could use:
    >
    > scanf("%[^\n]\n",line);
    >
    > but you should be sure you understand the result you would get
    > if end of file occurs before you encounter a \n .


    Also, since \n is a whitespace directive for the fscanf family, it
    will consume not only the newline (if it exists) but any following
    whitespace. If this scanf is used in a loop, for example, it will
    remove leading whitespace from all lines after the first. So again,
    this is not equivalent to gets().

    (It *is* similar to gets() in that, as presented, it can overflow
    the line object. There should be a maximum size specifier on that
    conversion directive.)

    --
    Michael Wojcik

    Even though there may be some misguided critics of what we're trying
    to do, I think we're on the wrong path. -- Reagan
    Michael Wojcik, Apr 27, 2006
    #5
    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. Dawn Minnis
    Replies:
    6
    Views:
    474
    Lawrence Kirby
    Feb 8, 2005
  2. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,330
    those who know me have no need of my name
    Apr 3, 2006
  3. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    655
    Richard Bos
    May 2, 2006
  4. Bernard Liang

    advanced scanf

    Bernard Liang, Jul 20, 2006, in forum: C Programming
    Replies:
    2
    Views:
    2,135
    Eric Sosman
    Jul 20, 2006
  5. Ioannis Vranos

    printf() and scanf() questions

    Ioannis Vranos, Mar 5, 2008, in forum: C Programming
    Replies:
    43
    Views:
    3,511
    Richard
    Mar 8, 2008
Loading...

Share This Page