%i或%d使用printf()在c中打印整数? [英] %i or %d to print integer in c using printf()?
问题描述
我只是在学习C而且由于涉及到iOS开发,我对Objective-C有一点了解,但是,在Objective-C中我使用的是 NSLog(@%i,x) ;
将变量 x 打印到控制台但是我一直在阅读一些C教程,他们说要使用%d
而不是%i
。
I am just learning C and I have a little knowledge of Objective-C due to dabbling in iOS development, however, in Objective-C I was using NSLog(@"%i", x);
to print the variable x to the console however I have been reading a few C tutorials and they are saying to use %d
instead of %i
.
printf(%d,x );
和 printf(%i,x);
都将 x 打印到控制台正确。
printf("%d", x);
and printf("%i", x);
both print x to the console correctly.
这些似乎都让我到了同一个地方,所以我问经验丰富的开发人员哪个更受欢迎?还有一个语义正确或正确吗?
These both seem to get me to the same place so I am asking the experienced developers which is preferred? Is one more semantically correct or is right?
推荐答案
与 printf()
一起使用时,它们完全等效。就个人而言,我更喜欢%d
,它更经常使用(我应该说它是 int
的惯用转换说明符 ?)。
They are completely equivalent when used with printf()
. Personally, I prefer %d
, it's used more often (should I say "it's the idiomatic conversion specifier for int
"?).
(%i
和%d $ c之间的一个区别$ c>是与
scanf()
一起使用时,那么%d
总是需要十进制整数,而%i
将 0
和 0x
前缀识别为八进制和十六进制,但是没有理智的程序员使用 scanf()
,所以这不应该是一个问题。)
(One difference between %i
and %d
is that when used with scanf()
, then %d
always expects a decimal integer, whereas %i
recognizes the 0
and 0x
prefixes as octal and hexadecimal, but no sane programmer uses scanf()
anyway so this should not be a concern.)
这篇关于%i或%d使用printf()在c中打印整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!