2015-10-03 28 views
0

我們如何在表格中排列這個值,以便地址值和名稱以正確的行出現在列中?如何在c中創建這個給定代碼的表?

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
int main() { 
int tuna = 20; 
printf("Adress \t Name \t Value \n"); 
printf("%p \t %s \t %d \n",&tuna , "tuna", tuna); 

int * pTuna = &tuna; 

printf("%p \t %s \t %d \n", pTuna, "tuna", tuna); 
printf("%p \t %s \t %p \n", &pTuna, "tuna", pTuna); 


_getch(); 
return 0; 
} 
+1

每個字段的最大長度是多少? – manetsus

+0

其變量的存儲地址可以是隨機的 –

回答

0

我修改了你的程序,如下所示。這裏%-15確保數據將被打印在15個字符的字段中,並用左縮進。 Of'course我假設這裏的數據是適合15個字符的字段。您可能需要根據您的要求進行更改。

#include <stdio.h> 
#include <stdlib.h> 
#include <conio.h> 
int main() { 
int tuna = 20; 
printf("%-15s %-15s %-15s \n","Address","Name","Value"); 
printf("%-15p %-15s %-15d \n",&tuna , "tuna", tuna); 

int * pTuna = &tuna; 

printf("%-15p %-15s %-10d \n", pTuna, "tuna", tuna); 
printf("%-15p %-15s %-15p \n", &pTuna, "tuna", pTuna); 


_getch(); 
return 0; 
} 

和輸出我得到:

Address   Name   Value   
0xbffff510  tuna   20    
0xbffff510  tuna   20   
0xbffff50c  tuna   0xbffff510  

我希望這是你所期待的。

+0

是的,但是在(%-15p)中基本上代表了什麼-15? –

+0

@MAhmadRana'-15'表示數據將在左側縮進的15個字段中顯示。 – Pawan

0

這裏,語句打印字符串,但打印至少15個字符("%-15s %-15s %-15s")。如果字符串較小,則在末尾添加「空白」,因此它看起來是「有序的」。

printf("%-15s %-15s %-15s \n","Address","Name","Value"); 
printf("%-15p %-15s %-15d \n",&tuna , "tuna", tuna); 

int * pTuna = &tuna; 

printf("%-15p %-15s %-10d \n", pTuna, "tuna", tuna); 
printf("%-15p %-15s %-15p \n", &pTuna, "tuna", pTuna); 
+0

這(-15)如何解決問題? –

+0

就像我上面說的那樣,-15意味着它會打印15個字符,但是因爲例如「金槍魚」沒有15個字符並且只有4個字符,所以你應該知道它會在它周圍增加空白字符,直到它有15個字符字符長度明智。 – Inconnu