re-Write printf()

Discussion in 'C Programming' started by riva, Jan 13, 2008.

  1. riva

    riva Guest

    riva, Jan 13, 2008
    #1
    1. Advertising

  2. riva

    Eric Sosman Guest

    riva wrote:
    > This is an interview question from: http://www.freshersworld.com/interview/technical_interview_C.htm
    >
    > Can You write a function similar to printf() ?


    Yes.

    > I can only think using putchar() and casting for such a thing.


    I think you're thinking along the wrong lines. Suggested
    reading: "The Standard C Library" by P.J. Plauger.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jan 13, 2008
    #2
    1. Advertising

  3. On Jan 13, 2:26 pm, riva <> wrote:

    > This is an interview question from:http://www.freshersworld.com/interview/technical_interview_C.htm
    >
    > Can You write a function similar to printf() ?
    >
    > I can only think using putchar() and casting for such a thing.


    you need to handle variable number of arguments (the ... in printf())

    you need to parse the format thingies (%d, %s etc.)

    then take the appropriate action


    --
    Nick Keighley
     
    Nick Keighley, Jan 13, 2008
    #3
  4. "riva" <> wrote in message
    > This is an interview question from:
    > http://www.freshersworld.com/interview/technical_interview_C.htm
    >
    > Can You write a function similar to printf() ?
    >
    > I can only think using putchar() and casting for such a thing.
    >

    Obviously you need some way of outputting characters to the screen. This
    could be an 8x8 bitmap to a memory-mapped device, or it could be built on
    top of a function like putchar().

    The rules for formatting are quite intricate and certainly non-trivial to
    implement. You can implement a good enough printf() replacement that accepts
    %d %c and %s modifiers easily enough. Converting floating point to
    human-readable characters is a bit more difficult, and realistically you
    have to support both width and precision for it to be of any real use, since
    people don't like to read long strings of non-significant characters.
    Extending your "good enough" printf() to a fully conforming one is quite a
    job, but bread and butter type work.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jan 13, 2008
    #4
  5. riva

    Guest

    On Jan 13, 5:24 pm, "Malcolm McLean" <> wrote:
    > "riva" <> wrote in message
    > > This is an interview question from:
    > >http://www.freshersworld.com/interview/technical_interview_C.htm

    >
    > > Can You write a function similar to printf() ?

    >
    > > I can only think using putchar() and casting for such a thing.

    >
    > Obviously you need some way of outputting characters to the screen. This
    > could be an 8x8 bitmap to a memory-mapped device, or it could be built on
    > top of a function like putchar().

    Then it would not be a printf().
    printf() writes to the file stream 'stdout'
    printf() does not guarantee that it will output *anything at all* to
    the 'screen'.
     
    , Jan 13, 2008
    #5
  6. riva

    Guest

    On Jan 13, 4:26 pm, riva <> wrote:
    > This is an interview question from:http://www.freshersworld.com/interview/technical_interview_C.htm
    >
    > Can You write a function similar to printf() ?
    >
    > I can only think using putchar() and casting for such a thing.

    printf is usually written as a macro or a warper function to vfprintf
    -- snip.c --
    #include <stdarg.h>

    int printf(const char * restrict fmt, ...) {
    va_list list;
    int i;
    va_start(list, fmt);
    i = vfprintf(stdout, fmt, list);
    va_end(list);
    return i;
    }
    -- snip.c --
    That shall do it.
     
    , Jan 13, 2008
    #6
  7. <> wrote in message
    > On Jan 13, 5:24 pm, "Malcolm McLean" <> wrote:
    >> "riva" <> wrote in message
    >> > This is an interview question from:
    >> >http://www.freshersworld.com/interview/technical_interview_C.htm

    >>
    >> > Can You write a function similar to printf() ?

    >>
    >> > I can only think using putchar() and casting for such a thing.

    >>
    >> Obviously you need some way of outputting characters to the screen. This
    >> could be an 8x8 bitmap to a memory-mapped device, or it could be built on
    >> top of a function like putchar().

    >
    > Then it would not be a printf().
    > printf() writes to the file stream 'stdout'
    > printf() does not guarantee that it will output *anything at all* to
    > the 'screen'.
    >

    You're asking whether it is possible to write a wholly generic, portable
    printf(). The answer is yes, on top of a function that writes a single
    character to stdout.
    However the questioner may or may not be looking for some understnading
    about how to implement the stdout stream, or something similar.

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
     
    Malcolm McLean, Jan 13, 2008
    #7
  8. riva

    Chris Dollin Guest

    riva wrote:

    > This is an interview question from:
    > http://www.freshersworld.com/interview/technical_interview_C.htm
    >
    > Can You write a function similar to printf() ?


    I can and I have -- for suitable values of "similar to".

    > I can only think using putchar() and casting for such a thing.


    It's very like that you'll need both of those, but they won't
    suffice on their lonesome ownsome.

    --
    Playing Oberon Hedgehog
    Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/
     
    Chris Dollin, Jan 13, 2008
    #8
  9. riva

    pete Guest

    riva wrote:
    >
    > This is an interview question from:
    > http://www.freshersworld.com/interview/technical_interview_C.htm
    >
    > Can You write a function similar to printf() ?
    >
    > I can only think using putchar() and casting for such a thing.


    /* BEGIN min_printf.c */
    /*
    ** min_printf uses the following 6 features from stdio.h:
    ** 1 EOF
    ** 2 FILE
    ** 3 stdout
    ** 4 putc
    ** 5 feof
    ** 6 ferror
    **
    ** min_printf uses the following 4 features from stdarg.h:
    ** 1 va_list
    ** 2 va_start
    ** 3 va_end
    ** 4 va_arg
    */
    #include <stdio.h>
    #include <stdarg.h>

    #define put_c(c, stream) putc((c), (stream))
    #define put_char(c) put_c((c), stdout)

    #define sput_c(c, stream) \
    (put_c((c), (stream)) == EOF \
    && (feof(stream) || ferror(stream)) ? EOF : 1)
    #define sfput_char(c) (sput_c((c), stdout))

    int (put_c)(int c, FILE *stream);
    int (put_char)(int c);
    /*
    ** 5 different conversion specifiers
    ** are supported by min_printf: %% %c %d %s %u
    ** and no fancy stuff.
    */
    int min_printf(const char *format, ...);

    static int sfput_d(int d);
    static int sfput_s(const char *s);
    static int sfput_u(unsigned u);
    static int sfput_u_plus_1(unsigned u);

    int (put_c)(int c, FILE *stream)
    {
    return put_c(c, stream);
    }

    int (put_char)(int c)
    {
    return put_char(c);
    }

    int min_printf(const char *format, ...)
    {
    int count, increment;
    va_list ap;

    va_start(ap, format);
    for (count = 0; *format != '\0'; ++format) {
    if (*format == '%') {
    switch (*++format) {
    case '%':
    increment = sfput_char('%');
    break;
    case 'c':
    increment = sfput_char(va_arg(ap, int));
    break;
    case 'd':
    increment = sfput_d(va_arg(ap, int));
    break;
    case 's':
    increment = sfput_s(va_arg(ap, char *));
    break;
    case 'u':
    increment = sfput_u(va_arg(ap, unsigned));
    break;
    default:
    increment = sfput_char(*format);
    break;
    }
    } else {
    increment = sfput_char(*format);
    }
    if (increment != EOF) {
    count += increment;
    } else {
    count = -42;
    break;
    }
    }
    va_end(ap);
    return count;
    }

    static int sfput_s(const char *s)
    {
    int count;

    for (count = 0; *s != '\0'; ++s) {
    if (sfput_char(*s) == EOF) {
    count = EOF;
    break;
    }
    ++count;
    }
    return count;
    }

    static int sfput_d(int d)
    {
    int count;

    if (0 > d) {
    count = put_char('-');
    if (count != EOF) {
    count = sfput_u_plus_1(-(d + 1));
    if (count != EOF) {
    ++count;
    }
    }
    } else {
    count = sfput_u(d);
    }
    return count;
    }

    static int sfput_u(unsigned u)
    {
    int count;
    unsigned digit, tenth;

    tenth = u / 10;
    digit = u - 10 * tenth + '0';
    count = tenth != 0 ? sfput_u(tenth) : 0;
    return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
    }

    static int sfput_u_plus_1(unsigned u)
    {
    int count;
    unsigned digit, tenth;

    tenth = u / 10;
    digit = u - 10 * tenth + '0';
    if (digit == '9') {
    if (tenth != 0) {
    count = sfput_u_plus_1(tenth);
    } else {
    count = put_char('1') == EOF ? EOF : 1;
    }
    digit = '0';
    } else {
    count = tenth != 0 ? sfput_u(tenth) : 0;
    ++digit;
    }
    return count != EOF && put_char(digit) != EOF ? count + 1 : EOF;
    }

    /* END min_printf.c */

    --
    pete
     
    pete, Jan 14, 2008
    #9
  10. J. J. Farrell, Jan 14, 2008
    #10
  11. riva

    pete Guest

    wrote:
    >
    > On Jan 13, 4:26 pm, riva <> wrote:
    > > This is an interview question from:http://www.freshersworld.com/interview/technical_interview_C.htm
    > >
    > > Can You write a function similar to printf() ?
    > >
    > > I can only think using putchar() and casting for such a thing.

    > printf is usually written as a macro or a warper function to vfprintf
    > -- snip.c --
    > #include <stdarg.h>
    >
    > int printf(const char * restrict fmt, ...) {
    > va_list list;
    > int i;
    > va_start(list, fmt);
    > i = vfprintf(stdout, fmt, list);
    > va_end(list);
    > return i;
    > }
    > -- snip.c --
    > That shall do it.


    That's an interesting idea.
    So, I just rewrote min_printf that way in my toy library:

    http://www.mindspring.com/~pfilandr/C/library/std_io.h
    http://www.mindspring.com/~pfilandr/C/library/std_io.c
    http://www.mindspring.com/~pfilandr/C/library/

    --
    pete
     
    pete, Jan 15, 2008
    #11
    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. Peter Hansen
    Replies:
    1
    Views:
    407
    Joona I Palaste
    Aug 24, 2003
  2. Peter Hansen
    Replies:
    1
    Views:
    432
    Joona I Palaste
    Aug 24, 2003
  3. ben
    Replies:
    4
    Views:
    657
    Martin Ambuhl
    Jun 26, 2004
  4. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
Loading...

Share This Page