gpt4 book ai didi

c - snprintf如何对齐字节

转载 作者:行者123 更新时间:2023-11-30 20:04:02 26 4
gpt4 key购买 nike

按照here的回答,我想看看 snprintf 使用了多少字节。代码如下

#include "stdio.h"

int main() {
printf("%d\n", snprintf(NULL, 0, "%d:%llx:%d:%llx:%llx:%llx", 0, 0, 0, 0, 0, 0));
printf("%d\n", snprintf(NULL, 0, "%d:%llx:%d:%llx:%llx:", 0, 0, 0, 0, 0));
printf("%d\n", snprintf(NULL, 0, "%llx", 0));
return 0;
}

返回

22
10
1

我不明白打印的其他数据末尾的%llx如何可以使用12个字节,而如果单独使用它只使用1个字节。 snprintf 是否进行字节对齐?

最佳答案

您的代码有一个错误。 %llx 格式说明符仅适用于 long long。尝试:

#include "stdio.h"

int main() {
printf("%d\n", snprintf(NULL, 0, "%d:%llx:%d:%llx:%llx:%llx", 0, 0LL, 0LL, 0LL, 0LL, 0LL));
printf("%d\n", snprintf(NULL, 0, "%d:%llx:%d:%llx:%llx:", 0, 0LL, 0LL, 0LL, 0LL));
printf("%d\n", snprintf(NULL, 0, "%llx", 0LL));
return 0;
}

关于c - snprintf如何对齐字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44351309/

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