C语言 %i 或 %d 使用 printf() 在 c 中打印整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17329647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:49:05  来源:igfitidea点击:

%i or %d to print integer in c using printf()?

cconsoleintegerintprintf

提问by Dummy Code

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 xto the console however I have been reading a few C tutorials and they are saying to use %dinstead of %i.

我只是在学习 C,由于涉足 iOS 开发,我对 Objective-C 有一点了解,但是,在 Objective-C 中,我使用NSLog(@"%i", x);将变量x打印到控制台,但是我一直在阅读一些 C 教程,它们说要使用%d而不是%i.

printf("%d", x);and printf("%i", x);both print xto the console correctly.

printf("%d", x);并且printf("%i", x);都正确地将x打印到控制台。

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?

这些似乎都让我到了同一个地方,所以我问有经验的开发人员哪个更受欢迎?在语义上是正确的还是正确的

回答by

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"?).

与 一起使用时,它们完全等效printf()。就我个人而言,我更喜欢%d它的使用频率更高(我应该说“它是”的惯用转换说明符int吗?)。

(One difference between %iand %dis that when used with scanf(), then %dalways expects a decimal integer, whereas %irecognizes the 0and 0xprefixes as octal and hexadecimal, but no sane programmer uses scanf()anyway so this should not be a concern.)

%iand之间的一个区别%d是,当与scanf(), then一起使用时,%d总是期望一个十进制整数,而%i0and0x前缀识别为八进制和十六进制,但scanf()无论如何没有理智的程序员使用,所以这不应该是一个问题。)

回答by A.s. Bhullar

I am just adding example here because I think examples make it easier to understand.

我只是在这里添加示例,因为我认为示例更容易理解。

In printf() they behave identically so you can use any either %d or %i. But they behave differently in scanf().

在 printf() 中,它们的行为相同,因此您可以使用任何 %d 或 %i。但是它们在 scanf() 中的行为不同。

For example:

例如:

int main()
{
    int num,num2;
    scanf("%d%i",&num,&num2);// reading num using %d and num2 using %i

    printf("%d\t%d",num,num2);
    return 0;
}

Output:

输出:

enter image description here

在此处输入图片说明

You can see the different results for identical inputs.

您可以看到相同输入的不同结果。

num:

num

We are reading numusing %dso when we enter 010it ignores the first 0and treats it as decimal 10.

我们正在阅读numusing %dso 当我们输入010它时会忽略第一个0并将其视为小数10

num2:

num2

We are reading num2using %i.

我们正在阅读num2使用%i.

That means it will treat decimals, octals, and hexadecimals differently.

这意味着它将以不同的方式处理十进制、八进制和十六进制。

When it give num2010it sees the leading 0and parses it as octal.

当它给出时,它会num2010看到前导0并将其解析为八进制。

When we print it using %dit prints the decimal equivalent of octal 010which is 8.

当我们使用%d它打印它时,它会打印八进制的十进制等价物,0108.

回答by ouah

dand iconversion specifiers behave the same with fprintfbut behave differently for fscanf.

di转换说明符的行为与 相同,fprintf但对于 的行为不同fscanf

As some other wrote in their answer, the idiomatic way to print an intis using dconversion specifier.

正如其他人在他们的回答中所写的那样,打印 an 的惯用方法int是使用d转换说明符。

Regarding ispecifier and fprintf, C99 Rationale says that:

关于i说明符 and fprintf,C99 基本原理说:

The %i conversion specifier was added in C89 for programmer convenience to provide symmetry with fscanf's %i conversion specifier, even though it has exactly the same meaning as the %d conversion specifier when used with fprintf.

%i 转换说明符是在 C89 中添加的,以方便程序员使用 fscanf 的 %i 转换说明符提供对称性,即使它在与 fprintf 一起使用时与 %d 转换说明符具有完全相同的含义。

回答by Stephan

%d seems to be the norm for printing integers, I never figured out why, they behave identically.

%d 似乎是打印整数的规范,我一直不知道为什么,它们的行为相同。

回答by Priyatham51

both %dand %ican be used to print an integer

%d%i可用于打印的整数

%d stands for "decimal", and %i for "integer." You can use %x to print in hexadecimal, and %o to print in octal.

%d 代表“十进制”,%i 代表“整数”。您可以使用 %x 以十六进制打印,使用 %o 以八进制打印。

You can use %i as a synonym for %d, if you prefer to indicate "integer" instead of "decimal."

如果您更喜欢表示“整数”而不是“十进制”,则可以使用 %i 作为 %d 的同义词。

On input, using scanf(), you can use use both %i and %d as well. %i means parse it as an integer in any base (octal, hexadecimal, or decimal, as indicated by a 0 or 0x prefix), while %d means parse it as a decimal integer.

在输入时,使用 scanf(),您也可以同时使用 %i 和 %d。%i 表示将其解析为任何基数(八进制、十六进制或十进制,由 0 或 0x 前缀表示)中的整数,而 %d 表示将其解析为十进制整数。

check here for more explanation

在这里查看更多解释

why does %d stand for Integer?

为什么 %d 代表整数?

回答by David Roundy

As others said, they produce identical output on printf, but behave differently on scanf. I would prefer %dover %ifor this reason. A number that is printed with %dcan be read in with %dand you will get the same number. That is not always true with %i, if you ever choose to use zero padding. Because it is common to copy printf format strings into scanf format strings, I would avoid %i, since it could give you a surprising bug introduction:

正如其他人所说,它们在 printf 上产生相同的输出,但在 scanf 上表现不同。由于这个原因,我宁愿%d超过%i。打印的数字%d可以读入,%d您将获得相同的数字。%i如果您曾经选择使用零填充,则情况并非总是如此。因为将 printf 格式字符串复制到 scanf 格式字符串中是很常见的,所以我会避免%i,因为它可能会给您一个令人惊讶的错误介绍:

I write fprintf("%i ...", ...);

我写的 fprintf("%i ...", ...);

You copy and write fscanf(%i ...", ...);

你抄写 fscanf(%i ...", ...);

I decide I want to align columns more nicely and make alphabetization behave the same as sorting: fprintf("%03i ...", ...);(or %04d)

我决定要更好地对齐列并使字母排序的行为与排序相同:( fprintf("%03i ...", ...);%04d

Now when you read my numbers, anything between 10 and 99 is interpreted in octal. Oops.

现在,当您阅读我的数字时,10 到 99 之间的任何内容都被解释为八进制。哎呀。

If you want decimal formatting, just say so.

如果你想要十进制格式,就这么说。