格式化 long 的 printf 的转换说明符是什么?

发布于 2024-07-05 21:46:08 字数 133 浏览 4 评论 0原文

printf 函数采用参数类型,例如 %d%i 表示 signed int。 但是,我没有看到任何 long 值。

The printf function takes an argument type, such as %d or %i for a signed int. However, I don't see anything for a long value.

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

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

发布评论

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

评论(7

孤凫 2024-07-12 21:46:09

我想你的意思是:

unsigned long n;
printf("%lu", n);   // unsigned long

或者

long n;
printf("%ld", n);   // signed long

I think you mean:

unsigned long n;
printf("%lu", n);   // unsigned long

or

long n;
printf("%ld", n);   // signed long
手心的温暖 2024-07-12 21:46:09

在大多数平台上,longint 的大小相同(32 位)。 尽管如此,它确实有自己的格式说明符:

long n;
unsigned long un;
printf("%ld", n); // signed
printf("%lu", un); // unsigned

对于 64 位,您需要一个 long long

long long n;
unsigned long long un;
printf("%lld", n); // signed
printf("%llu", un); // unsigned

哦,当然,它在 Windows 中有所不同:

printf("%l64d", n); // signed
printf("%l64u", un); // unsigned

经常,当我打印 64 位时值,我发现以十六进制打印它们很有帮助(通常数字很大,它们是指针或位字段)。

unsigned long long n;
printf("0x%016llX", n); // "0x" followed by "0-padded", "16 char wide", "long long", "HEX with 0-9A-F"

将打印:

0x00000000DEADBEEF

顺便说一句,“长”不再意味着那么多(在主流 x64 上)。 “int”是平台默认的 int 大小,通常为 32 位。 “长”通常是相同的尺寸。 然而,它们在旧平台(和现代嵌入式平台!)上具有不同的可移植性语义。 “long long”是一个 64 位数字,通常是人们想要使用的数字,除非他们真的知道他们正在编辑一段 x 平台可移植代码。 即使如此,他们也可能会使用宏来捕获类型的语义(例如 uint64_t)。

char c;       // 8 bits
short s;      // 16 bits
int i;        // 32 bits (on modern platforms)
long l;       // 32 bits
long long ll; // 64 bits 

过去,“int”是 16 位。 您可能会认为现在是 64 位,但事实并非如此,这会导致严重的可移植性问题。 当然,这也是对神秘且历史悠久的事实的简化。 请参阅 wiki:Integer

On most platforms, long and int are the same size (32 bits). Still, it does have its own format specifier:

long n;
unsigned long un;
printf("%ld", n); // signed
printf("%lu", un); // unsigned

For 64 bits, you'd want a long long:

long long n;
unsigned long long un;
printf("%lld", n); // signed
printf("%llu", un); // unsigned

Oh, and of course, it's different in Windows:

printf("%l64d", n); // signed
printf("%l64u", un); // unsigned

Frequently, when I'm printing 64-bit values, I find it helpful to print them in hex (usually with numbers that big, they are pointers or bit fields).

unsigned long long n;
printf("0x%016llX", n); // "0x" followed by "0-padded", "16 char wide", "long long", "HEX with 0-9A-F"

will print:

0x00000000DEADBEEF

Btw, "long" doesn't mean that much anymore (on mainstream x64). "int" is the platform default int size, typically 32 bits. "long" is usually the same size. However, they have different portability semantics on older platforms (and modern embedded platforms!). "long long" is a 64-bit number and usually what people meant to use unless they really really knew what they were doing editing a piece of x-platform portable code. Even then, they probably would have used a macro instead to capture the semantic meaning of the type (eg uint64_t).

char c;       // 8 bits
short s;      // 16 bits
int i;        // 32 bits (on modern platforms)
long l;       // 32 bits
long long ll; // 64 bits 

Back in the day, "int" was 16 bits. You'd think it would now be 64 bits, but no, that would have caused insane portability issues. Of course, even this is a simplification of the arcane and history-rich truth. See wiki:Integer

假扮的天使 2024-07-12 21:46:09

这取决于,如果您指的是 unsigned long,则格式化字符为 "%lu"。 如果您指的是有符号长整型,则格式字符为 "%ld"

It depends, if you are referring to unsigned long the formatting character is "%lu". If you're referring to signed long the formatting character is "%ld".

单身狗的梦 2024-07-12 21:46:09

我需要打印 unsigned long long,所以我发现这有效:

unsigned long long n;
printf("%llu", n);

对于所有其他组合,我相信您使用 printf 手册,先获取行,然后获取要打印的任何类型的列标签(就像我使用 printf("%llu", n) 所做的那样上面)。

I needed to print unsigned long long, so I found this works:

unsigned long long n;
printf("%llu", n);

For all other combinations, I believe you use the table from the printf manual, taking the row, then column label for whatever type you're trying to print (as I do with printf("%llu", n) above).

天涯离梦残月幽梦 2024-07-12 21:46:09

我认为要明确回答这个问题需要知道您正在使用的编译器名称和版本以及它正在编译的平台(CPU 类型、操作系统等)。

I think to answer this question definitively would require knowing the compiler name and version that you are using and the platform (CPU type, OS etc.) that it is compiling for.

菩提树下叶撕阳。 2024-07-12 21:46:08

l(小写字母 L)直接放在说明符之前。

unsigned long n;
long m;

printf("%lu %ld", n, m);

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

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