views:

71

answers:

5

Usually usage of function:

my_func("test");

Whether I can use this parameter so?

my_func(printf("current_count_%d",ad_id));

int my_func(const char *key)

+1  A: 

No; printf() returns the number of characters printed to stdout. Use s[n]printf() to create the string, then pass that.

Ignacio Vazquez-Abrams
Remember that you will have to manually declare a `char[] buffer` and release it when you're done with it.
Christian Mann
Or just use an array of a statically declared length, like char buffer[50] or whatever.
Tommy
That's right, because they're primitives, not objects. My bad.
Christian Mann
A: 

The function printf returns an integer.

int printf ( const char * format, ... );

Hence, you can use it in my_func as long as my_func takes an integer as parameter. This does not seem to be the case.

Daniel Daranas
+5  A: 

Yes you can use the return value of printf as a function parameter.
But remember printf on success returns the number of characters written.
So

foo(printf("bar%d",123)); 

passes 6 to the foo function not bar123.

If you want to pass the string that printf prints, you can make use of sprintf function. It is similar to printf but instead of writing to the console it writes into a char array.

codaddict
+2  A: 
    char buf[64]; /* malloc or whatever */
    int pos = snprintf(buf, sizeof(buf), "current_count_%d", ad_id);
    if (sizeof(buf) <= pos)
                    buf[sizeof(buf)-1] = '\0';
    my_func(buf)
rib
+1 for properly terminating a NUL-less outcome of `snprintf`
pmg
Having done a quick bit of research, it's only on Windows that snprintf fails to add a terminating NULL if it truncates the output. So I wouldn't be surprised if this is a bug or side effect of Microsoft's disinterest in post-C89 developments.
Tommy
You are correct.
rib
A: 

If you're looking to pass a variable number of arguments, like printf accepts, to some function then you need to look into . To reproduce printf (for the sake of argument):

void varargfunc(char *fmt, ...)
{
    char formattedString[2048]; /* fixed length, for a simple example - and
                                   snprintf will keep us safe anyway */
    va_list arguments;

    va_start(arguments, fmt); /* use the parameter before the ellipsis as
                                 a seed */

    /* assuming the first argument after fmt is known to be an integer,
       you could... */
    /*
        int firstArgument = va_arg(arguments, int);
        fprintf(stderr, "first argument was %d", firstArgument);
    */

    /* do an vsnprintf to get the formatted string with the variable
       argument list. The v[printf/sprintf/snprintf/etc] functions
       differ from [printf/sprintf/snprintf/etc] by taking a va_list
       rather than ... - a va_list can't turn back into a ... because
       it doesn't inherently know how many additional arguments it
       represents (amongst other reasons) */
    vsnprintf(formattedString, 2048, fmt, arguments);

    /* formattedString now contains the first 2048 characters of the
       output string, with correct field formatting and a terminating
       NULL, even if the real string would be more than 2047 characters
       long. So put that on stdout. puts adds an additional terminating
       newline character, so this varies a little from printf in that 
       regard. */
    puts(formattedString);

    va_end(arguments); /* clean up */
}

If you wanted to add additional arguments that aren't related to the format, add them before the 'fmt' argument. Fmt is passed to va_start to say "the variable arguments start after this one".

Tommy