%di 说明符在 C 编程中有什么作用?

What does %di specifier do in C programming?

printf 函数中的

%d 说明符意味着我们要将变量显示为十进制整数,而 %f 说明符会将其显示为浮点数,依此类推。 但是 %di 说明符有什么作用?

I found this specifier in this program(C program to add, subtract, multiply and divide Complex Numbers, complex arithmetic)

例如,

printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);

没有说明符"%di"。所以,它的工作原理是 "%d",在它后面有一个字母 "i"(这是打印复数虚部的标准方法)。

所以当你有一行 printf("Sum of two complex numbers = %d + %di",c.real,c.img); 时,它是这样工作的:

  • 它打印 "Sum of two complex numbers = ",
  • 它打印值 c.real,
  • 它打印“+”,
  • 它打印值 c.img,
  • 并打印 "i".

C 中没有 %di 这样的东西!事实上 %d 已经在整数代码中使用了!由于您要打印复数 如

4 + 5i

您需要在打印 5 后立即打印一个 'i,因此使用“%di”和“%d”并且 printf 函数会单独看到 i!

如果 temp/temp3 的计算结果为 25,而 temp2/temp3 的计算结果为 15,则行

printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);

将打印

Division of two complex numbers = 25 15i

上面格式说明符中的i打印字母i%d 部分打印数字。在上述格式说明符中使用 i 的唯一目的是以更加用户友好的方式打印复数的实部和虚部。

在 C 中没有说明符 "%di"。它只是作为 "%d" 正常工作,在它之后你有一个字母 "i" 打印。例如你的输出看起来像这样:

int temp1=8;
int temp2=10;
int temp3=2;

printf("Division of two complex numbers = %d %di",temp1/temp3,temp2/temp3);

输出:Division of two complex numbers = 4 5i