gpt4 book ai didi

我可以像 printf 那样存储格式化的字符串吗?

转载 作者:太空宇宙 更新时间:2023-11-04 01:34:31 26 4
gpt4 key购买 nike

在 C 中有没有一种方法可以像在 printf 中那样格式化一个字符串,而不是将它存储在一个变量中?我正在尝试将其用于系统调用,因此我可以在调用中包含一个变量。

最佳答案

您可以使用 sprintf (或 C99/C11 中的 snprintf)在使用前格式化您的字符串。

int sprintf(char *str, const char *format, ...);
int snprintf(char *str, size_t size, const char *format, ...);

例子:

 // Use the value of an integer 'n' as argument in 'perror' (C99).

#include <limits.h>
#include <stdio.h>

char argument[sizeof(int) * CHAR_BIT + 1];
snprintf(argument, sizeof argument, "%d", n);
perror(argument);

关于我可以像 printf 那样存储格式化的字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16969397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com