为什么%d个代表整数? [英] why does %d stand for Integer?

查看:1250
本文介绍了为什么%d个代表整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起​​来并不多产,但是我正在寻找一种方式来记住所有的格式codeS为的printf 来电。 %S %P %F 都是明显,但我不明白的地方%d个从何而来。为%I 已经采取别的东西吗?

I know this doesn't sound productive, but I'm looking for a way to remember all of the formatting codes for printf calls. %s, %p, %f are all obvious, but I can't understand where %d comes from. Is %i already taken by something else?

推荐答案

表示十进制(基数为10 ),而不是整数。您可以使用%X 打印(基数为16)和%ο(基数为8)打印。一个整数,可以在这些基地。

It stands for "decimal" (base 10), not "integer." You can use %x to print in hexadecimal (base 16), and %o to print in octal (base 8). An integer could be in any of these bases.

的printf(),您可以用%I 作为一个同义词% ð,如果你preFER表示整数而不是小数,但%d个一般$ pferred,因为它是p&$更具体。

In printf(), you can use %i as a synonym for %d, if you prefer to indicate "integer" instead of "decimal," but %d is generally preferred as it's more specific.

在输入,使用 scanf()的,你可以使用同时使用%I %d个为好。 %I 表示解析它在任何基地一个整数(八进制,十六进制或十进制,以指示 0 0X preFIX),而%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.

下面是在行动所有这些的例子:

Here's an example of all of them in action:

#include <stdio.h>

int main() {
  int out = 10;
  int in[4];

  printf("%d %i %x %o\n", out, out, out, out);
  sscanf("010 010 010 010", "%d %i %x %o", &in[0], &in[1], &in[2], &in[3]);
  printf("%d %d %d %d\n", in[0], in[1], in[2], in[3]);
  sscanf("0x10 10 010", "%i %i %i", &in[0], &in[1], &in[2]);
  printf("%d %d %d\n", in[0], in[1], in[2]);

  return 0;
}

所以,你应该只使用%I 如果你想输入的基础依赖于preFIX;如果输入的基础应该是固定的,你应该使用%d个%X %ο。尤其是,事实上,一个领先的 0 使您可以八进制方式可以赶上你。

So, you should only use %i if you want the input base to depend on the prefix; if the input base should be fixed, you should use %d, %x, or %o. In particular, the fact that a leading 0 puts you in octal mode can catch you up.

这篇关于为什么%d个代表整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆