在使用"snprintf"时,使用sizeof运算符是否可以?例如。
char cstring[20];
snprintf(cstring,sizeof(cstring),"%s","somestring......");
char cstring[20];
snprintf(cstring,sizeof(cstring),"%s","somestring......");
没问题,你提供的具体案例很好,除了你没有检查返回值,所以你不会知道字符串是否被截断。
你所发布的示例看起来不错。
但是,在任何数组退化为指针的情况下,这种写法都是不正确的:
void func(char s []) {
snprintf(s,sizeof(s),"%s","somestring......"); // Not fine, s is actually pointer
}
int main(void) {
char cstring[20];
func(cstring); // Array decays to pointer
在 snprintf
中您可以使用 sizeof
操作符,但如果字符串的长度大于您指定的大小,则字符串中剩余的字符将丢失。
是的,你可以使用。但是如果字符串长度大于sizeof值,则该字符串将被截断,或者最多存储给定值在该数组中。
#include <stdio.h>
int main()
{
char str[16];
int len;
len = snprintf(str, sizeof( str ), "%s %d %f", "hello world", 1000, 10.5);
printf("%s\n", str);
if (len >= 16)
{
printf("length truncated (from %d)\n", len);
}
}
output:
=======
./a.out
hello world 100
length truncated (from 26)
/* You can see from the output only 15 char + '\0' is displayed on the console ( stored in the str ) */
/* Now changing the size of the str from 16 to 46 */
#include <stdio.h>
int main()
{
char str[46];
int len;
len = snprintf(str, sizeof( str ), "%s %d %f", "hello world", 1000, 10.5);
printf("%s\n", str);
if (len >= 46)
{
printf("length truncated (from %d)\n", len);
}
}
output:
==========
./a.out
hello world 1000 10.500000
c++
标签标记了你的问题。实际上,在 C++ 中,如果有可能,我们不会使用snprintf
而是使用std::ostringstream
。如果你只是对 C 解决方案感兴趣,请考虑删除c++
标签。 - 5gon12edercstring
没有被初始化。所以根据我几乎没有的 C++ 知识,sizeof(cstring)
可能是0
、20
或未定义的。 - Eric Duminilstrlen
会,对吧? - Eric Duminil