是否有任何 C 标准函数将值"1"传递给所有 (%s)

Is there any C standard function to pass a value "1" to all (%s)

本文关键字:标准 函数 是否 任何      更新时间:2023-10-16

我只想知道是否有任何C(或C++(标准函数将值"1"传递给所有(%s(。

sprintf(buffer, "record_id(%s)record_num(%s)record_val (%s"), "1"));

而不是传递喜欢和复制"1":

sprintf(query_buffer, "record_id(%s)record_num(%s)record_val (%s"), "1", "1", "1"));

我认为你可以这样格式化它;

sprintf( buffer, "record_id(%s)record_num(%1$s)record_val (%1$s"), "1") );

这是 printf 格式字符串的 POSIX 扩展。 您可以使用 n$ 来指示列表中的第 n 个参数。

No.

没有C标准函数。 至于C++(因为你说 cpp 函数(,我不确定,但我非常怀疑它是否存在。

不,标准中没有这样的东西。另请查看此和此以获取更多信息。

您将不得不寻找执行此操作的库。

例如,在C++中,这可能是提升:

#include <boost/format.hpp>
int main ()
{
std::cout << boost::format("%1%.%1%.%2%") % 12 % 2014 << 'n';
}

输出12.12.2014

不!但是您可以创建自己的函数,该函数接收数字字符串并将其放入%s...