printf 作为函数的参数

发布于 2024-09-29 06:14:34 字数 189 浏览 3 评论 0原文

函数的通常用法:

my_func("test");

那么我是否可以使用这个参数呢?

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

int my_func(const char *key)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

诠释孤独 2024-10-06 06:14:34

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

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

传递 6foo 函数而不是 bar123

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

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.

找个人就嫁了吧 2024-10-06 06:14:34
    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)
    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)
谁的新欢旧爱 2024-10-06 06:14:34

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

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

够钟 2024-10-06 06:14:34

如果您希望将可变数量的参数(例如 printf 接受的参数)传递给某个函数,那么您需要查看 .重现 printf (为了论证):

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 */
}

如果您想添加与格式无关的其他参数,请将它们添加在“fmt”参数之前。 Fmt 被传递给 va_start 来表示“变量参数在这个之后开始”。

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".

灯角 2024-10-06 06:14:34

函数 printf 返回一个整数

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文