doubleprintf中正确的格式说明符是什么?是它%f还是它%lf?我相信它%f,但我不确定.
#include <stdio.h>
int main()
{
double d = 1.4;
printf("%lf", d); // Is this wrong?
}
Run Code Online (Sandbox Code Playgroud) #include <stdio.h>
int main() {
unsigned long long int num = 285212672; //FYI: fits in 29 bits
int normalInt = 5;
printf("My number is %d bytes wide and its value is %ul. A normal number is %d.\n", sizeof(num), num, normalInt);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
My number is 8 bytes wide and its value is 285212672l. A normal number is 0.
Run Code Online (Sandbox Code Playgroud)
我认为这个意想不到的结果是来自打印unsigned long long int.你怎么printf()了unsigned long long int?
我永远无法理解如何unsigned long在C中打印数据类型.
假设unsigned_foo是unsigned long,然后我尝试:
printf("%lu\n", unsigned_foo)printf("%du\n", unsigned_foo)printf("%ud\n", unsigned_foo)printf("%ll\n", unsigned_foo)printf("%ld\n", unsigned_foo)printf("%dl\n", unsigned_foo)而且他们都打印了某种-123123123数字而不是unsigned long我的数字.
%d和%i用作格式说明符的区别是什么printf?
我正在尝试打印类似off_t和size_t.什么是正确的占位符printf() 是可移植的?
或者是否有完全不同的方式来打印这些变量?
在我正在阅读的一本书中,写printf了一个单一的参数(没有转换说明符)被弃用了.它建议替代
printf("Hello World!");
Run Code Online (Sandbox Code Playgroud)
同
puts("Hello World!");
Run Code Online (Sandbox Code Playgroud)
要么
printf("%s", "Hello World!");
Run Code Online (Sandbox Code Playgroud)
谁能告诉我为什么printf("Hello World!");是错的?书中写道它包含漏洞.这些漏洞是什么?
我想size_t在C中打印出一个类型的变量,但似乎size_t在不同的体系结构上别名为不同的变量类型.例如,在一台计算机(64位)上,以下代码不会抛出任何警告:
size_t size = 1;
printf("the size is %ld", size);
Run Code Online (Sandbox Code Playgroud)
但在我的另一台机器(32位)上面的代码会产生以下警告消息:
警告:格式'%ld'需要类型'long int*',但参数3的类型为'size_t*'
我怀疑这是由于指针大小的不同,所以在我的64位机器size_t上别名为a long int("%ld"),而在我的32位机器size_t上别名为另一种类型.
是否有专门用于的格式说明符size_t?
c size-t platform-independent format-specifiers format-string
除了%hn和%hhn(指定h或hh指定指向对象的大小),格式说明符的h和hh修饰符有printf什么意义?
由于标准要求的默认促销适用于可变函数,因此不可能将类型char或short(或其任何有符号/无符号变体)的参数传递给printf.
根据7.19.6.1(7),h修饰符:
指定以下d,i,o,u,x或X转换规范适用于short int或unsigned short int参数(该参数将根据整数提升进行提升,但其值应转换为short int或打印前的unsigned short int); 或者后续的n转换规范适用于指向short int参数的指针.
如果参数实际上是类型short或unsigned short,则促销int后转换回short或unsigned short将产生与促销相同的值int而不进行任何转换.因此,对于类型的参数short或unsigned short,%d,%u等应给予相同的结果到%hd,%hu等.(并且同样地对于char类型和hh).
据我所知,h或者hh修饰符可能有用的唯一情况是当参数将其传递int到范围之外时,short或者unsigned short …
在我必须维护的一些代码中,我看到了一个格式说明符%*s.任何人都可以告诉我这是什么以及为什么使用它?
它的用法示例如下:
fprintf(outFile, "\n%*s", indent, "");
Run Code Online (Sandbox Code Playgroud) c ×10
printf ×8
c++ ×1
double ×1
long-integer ×1
long-long ×1
portability ×1
promotions ×1
puts ×1
scanf ×1
security ×1
size-t ×1
syntax ×1
unsigned ×1