C printf作为函数的参数

C printf作为函数的参数,c,function,arguments,printf,argument-passing,C,Function,Arguments,Printf,Argument Passing,通常使用函数: my_func("test"); 我是否可以这样使用这个参数 my_func(printf("current_count_%d",ad_id)); int my_func(常量字符*键)否printf()返回打印到标准输出的字符数。使用s[n]printf()创建字符串,然后传递该字符串。函数printf 因此,只要my_func采用整数作为参数,就可以在my_func中使用它。情况似乎并非如此。是的,您可以使用printf的返回值作为函数参数。但请记住printf成功返回写

通常使用函数:

my_func("test");
我是否可以这样使用这个参数

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

int my_func(常量字符*键)

printf()
返回打印到标准输出的字符数。使用
s[n]printf()
创建字符串,然后传递该字符串。

函数
printf


因此,只要
my_func
采用整数作为参数,就可以在
my_func
中使用它。情况似乎并非如此。

是的,您可以使用
printf
的返回值作为函数参数。
但请记住
printf
成功返回写入的字符数。
因此

foo(printf("bar%d",123)); 
6
传递到
foo
函数not
bar123

如果要传递
printf
打印的字符串,可以使用
sprintf
函数。它类似于
printf
,但不是写入控制台,而是写入字符数组。

char buf[64];/*马洛克还是什么*/
    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)
int pos=snprintf(基本单位,大小单位(基本单位),“当前计数%d”,ad\U id);
如果(sizeof(buf)如果希望将可变数量的参数(如printf accepts)传递给某个函数,则需要查看。要复制printf(为了参数):


如果要添加与格式无关的其他参数,请将它们添加到“fmt”参数之前。fmt传递给va_start,表示“变量参数在此参数之后开始”。

请记住,您必须手动声明一个
char[]缓冲区
并在完成后释放它。或者只使用静态声明长度的数组,如char buffer[50]或其他。没错,因为它们是原语,而不是对象。我的错。请指定您试图实现的内容。什么是
My_func
强度?couse的sprintf…int My_func(const char*key)经过快速的研究,只有在Windows上,如果snprintf截断了输出,它才能添加一个终止NULL。因此,如果这是一个bug或微软对C89后开发不感兴趣的副作用,我不会感到惊讶。
    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)
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 */
}