2015-10-06 179 views
-2

%printf函數內部的d說明符意味着我們要將變量顯示爲十進制整數,而%f說明符會將其顯示爲浮點數等等。 但是%di說明符的作用是什麼?%di說明符在C編程中做了什麼?

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); 
+6

這只是一個'%D',緊跟着的一個'i',可能指示虛部。只有'%d'是格式說明符,'i'字面地打印爲 – dhke

+1

,就像你寫'printf(「%di」,a)'那麼將打印'a'的第一個值,然後'i'一個整數變量。) – ameyCU

+1

你試過了嗎? –

回答

2

沒有說明符"%di"。所以,它的作用是"%d",之後你有一個字母"i"(這是打印複數值的虛部的標準方法)。

所以,當你有一個線printf("Sum of two complex numbers = %d + %di",c.real,c.img);它的工作原理是這樣的:

  • 它打印出 「的兩個複數= SUM」,
  • 它打印值c.real
  • 它打印出 「+」,
  • 它打印值c.img,
  • 而且它打印"i"
2

在C中沒有%di這樣的東西!實際上%d已經用在整數的代碼中了!由於要打印的複數 如

4 + 5I

需要打印一個「i打印5因此‘%二’用於‘%d’之後立即和我分別被printf函數看到!

+0

「_被編譯器_單獨看到:」不是由編譯器,而是由'printf'函數。 –

+0

^是的更合適!編輯 –

3

如果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的唯一目的是以更友好的方式打印複數的實部和虛部。

0

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