[英]How to create a table of this given code in 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;
}
我修改了您的程序,如下所示。 在这里, %-15
确保以15个字符的字段以左缩进打印数据。 当然,我在这里假设的数据将适合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
我希望这是您想要的。
在这里,该语句打印字符串,但至少打印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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.