Variadic functions

Discussion in 'C Programming' started by Christopher Benson-Manica, Nov 4, 2004.

  1. Do variadic functions (such as printf()) always scan the format string
    for format specifiers, even when there are no additional arguments?
    If it is instead a QoI issue, what would be true for a typical
    implementation?

    Basically, I'm wondering whether there is any advantage to using
    puts() instead of printf() for output of a string.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Nov 4, 2004
    #1
    1. Advertising

  2. Christopher Benson-Manica wrote:
    > Do variadic functions (such as printf()) always scan the format string
    > for format specifiers, even when there are no additional arguments?
    > If it is instead a QoI issue, what would be true for a typical
    > implementation?
    >
    > Basically, I'm wondering whether there is any advantage to using
    > puts() instead of printf() for output of a string.


    In general case functions have no means to determine how many arguments
    was supplied in the call. In other words, the situation "when there are
    no additional arguments" is not detectable from inside of the function,
    which means that the function has no other choice but to scan the format
    string and try to retrieve the variadic parameters (if format string
    directs it to do so). If there were no actual arguments in the call, the
    behavior is undefined.

    --
    Best regards,
    Andrey Tarasevich
     
    Andrey Tarasevich, Nov 4, 2004
    #2
    1. Advertising

  3. >Do variadic functions (such as printf()) always scan the format string
    >for format specifiers, even when there are no additional arguments?


    There is no standard way for a varadic function to know how many
    arguments it actually got. Now, printf() *COULD* be implemented
    with compiler magic, but I can't see where this particular optimization
    is worth doing. And printf() would STILL have to translate %% in
    the format string to % in the output.

    >If it is instead a QoI issue, what would be true for a typical
    >implementation?


    In a typical implementation, printf() does not know how many arguments
    it actually got, and the way it tells how many arguments it's
    supposed to have is to scan the format string.

    >Basically, I'm wondering whether there is any advantage to using
    >puts() instead of printf() for output of a string.


    printf(string) and puts(string) are not equivalent. puts() adds a
    trailing newline; printf() does not. printf(string) is downright
    dangerous if string can contain arbitrary data obtained from input
    that hasn't been screened for % characters (funny input can make
    the program smegfault). printf("%s\n", string) is equivalent to
    puts(string) when the return value is not used, but this doesn't
    involve invoking printf() with no arguments beyond the format string.

    Any program can run infinitely fast if it's acceptable for it to
    give the wrong (no) result.

    Gordon L. Burditt
     
    Gordon Burditt, Nov 4, 2004
    #3
  4. In article <cmee1s$n2p$>,
    Christopher Benson-Manica <> wrote:
    >Do variadic functions (such as printf()) always scan the format string
    >for format specifiers, even when there are no additional arguments?
    >If it is instead a QoI issue, what would be true for a typical
    >implementation?


    Mu. Barring implementation magic in printf, the only way it has to
    tell that there are(should be) no additional arguments is to scan the
    format string. In the general case, a variadic function needs to be
    able to determine the availability and type of variadic arguments using
    only information in the non-variadic arguments.

    >Basically, I'm wondering whether there is any advantage to using
    >puts() instead of printf() for output of a string.


    puts() handles '%' in the string rather more nicely, by simply printing
    it instead of invoking undefined behavior. It also appends a newline,
    which may be undesirable.
    If you can get away with not using printf *at all*, you can avoid bringing
    the entire printf engine into your program. Using printf somewhere means
    using it somewhere else has no additional size cost. Linking against
    dynamic libraries will make this potential benefit irrelevant.
    Using printf will have a penalty of maybe a few cycles per character
    for strings that don't contain any '%'. With the additional cost of
    actually moving the characters between the program and whatever stdout
    is connected to, there's no way you'll ever notice this cost, no matter
    how many characters you're printing.

    printf("%s\n",str) behaves exactly the same as puts(str). If the trailing
    newline is undesirable and str may contain '%', you want printf("%s",str).


    dave

    --
    Dave Vandervies
    I'm ashamed to admit it, but I actually thought of this possibility when
    writing the code shown. That means I've been hanging around comp.lang.c
    *much* too long. --Eric Sosman in comp.lang.c
     
    Dave Vandervies, Nov 4, 2004
    #4
  5. Christopher Benson-Manica

    pete Guest

    Christopher Benson-Manica wrote:
    >
    > Do variadic functions (such as printf()) always scan the format string
    > for format specifiers, even when there are no additional arguments?
    > If it is instead a QoI issue, what would be true for a typical
    > implementation?
    >
    > Basically, I'm wondering whether there is any advantage to using
    > puts() instead of printf() for output of a string.


    K&R2, section 7.7, page 156,
    shows a function definition for minprintf().
    It should give you some idea of the logic invlolved.
    The logic of puts is relatively simple.

    In my toy library, I have put_s like this:

    #include <stdio.h>
    #define put_c(c, stream) (putc((c), (stream)))
    #define put_char(c) (put_c((c), stdout))

    int put_s(const char *s)
    {
    while (*s != '\0') {
    if (put_char(*s) == EOF && ferror(stdout) != 0) {
    return EOF;
    }
    ++s;
    }
    return put_char('\n');
    }

    My min_printf is like this:

    #include <stdarg.h>

    #define fsput_char(c) \
    (put_char(c) == EOF && ferror(stdout) != 0 ? EOF : 1)

    static int fsput_s(const char *);
    static int fsput_u(unsigned);
    static int fsput_u_plus_1(unsigned);
    static int fsput_d(int);

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

    va_start(ap, s);
    for (count = 0; *s != '\0'; ++s) {
    if (*s == '%') {
    switch (*++s) {
    case 'c':
    increment = fsput_char(va_arg(ap, int));
    break;
    case 'd':
    increment = fsput_d(va_arg(ap, int));
    break;
    case 's':
    increment = fsput_s(va_arg(ap, char *));
    break;
    case 'u':
    increment = fsput_u(va_arg(ap, unsigned));
    break;
    default:
    increment = fsput_char(*s);
    break;
    }
    } else {
    increment = fsput_char(*s);
    }
    if (increment != EOF) {
    count += increment;
    } else {
    count = EOF;
    break;
    }
    }
    va_end(ap);
    return count;
    }

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

    for (count = 0; *s != '\0'; ++s) {
    if (put_char(*s) == EOF && ferror(stdout) != 0) {
    return EOF;
    }
    ++count;
    }
    return count;
    }

    static int fsput_u(unsigned integer)
    {
    int count;
    unsigned digit, tenth;

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

    static int fsput_u_plus_1(unsigned integer)
    {
    int count;
    unsigned digit, tenth;

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

    static int fsput_d(int integer)
    {
    int count;

    if (0 > integer) {
    if (put_char('-') == EOF) {
    return EOF;
    }
    count = fsput_u_plus_1(-(integer + 1));
    return count != EOF ? count + 1 : EOF;
    } else {
    return fsput_u(integer);
    }
    }

    --
    pete
     
    pete, Nov 5, 2004
    #5
  6. Christopher Benson-Manica

    Dan Pop Guest

    In <cmee1s$n2p$> Christopher Benson-Manica <> writes:

    >Do variadic functions (such as printf()) always scan the format string
    >for format specifiers, even when there are no additional arguments?
    >If it is instead a QoI issue, what would be true for a typical
    >implementation?


    The only source of information WRT the number and type of arguments for
    printf and friends is the format string. It is OK to pass them more
    arguments than specified by the format string (they will ignore them)
    but it invokes undefined behaviour to pass them fewer arguments.

    >Basically, I'm wondering whether there is any advantage to using
    >puts() instead of printf() for output of a string.


    Yes: puts doesn't have to scan the string (except for finding
    the terminating null character) and you don't have to tread the %
    character in a special way. As an added bonus, it also appends a
    free newline at the end of the string (use fputs if you don't want it).

    OTOH, the program is (slightly) more readable if printf is the only
    output function being used.

    However, both pro and con arguments are extremely weak, so use whatever
    you feel as the most appropriate to the context.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Currently looking for a job in the European Union
     
    Dan Pop, Nov 5, 2004
    #6
    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. Loony

    variadic functions

    Loony, Sep 12, 2003, in forum: C++
    Replies:
    2
    Views:
    366
    Kevin Goodsell
    Sep 12, 2003
  2. Colin Walters
    Replies:
    2
    Views:
    527
    Ben Pfaff
    Feb 13, 2004
  3. Ross A. Finlayson
    Replies:
    19
    Views:
    604
    Keith Thompson
    Mar 10, 2005
  4. Replies:
    2
    Views:
    353
    Dave Thompson
    Feb 27, 2006
  5. Replies:
    5
    Views:
    368
Loading...

Share This Page