[英]How to align the cursor to the left side after using printf (c linux)
[英]How to align with printf in C
所以這是一個簡單的問題,但我不知道如何。 那么如何用 %(number)s 左對齊或居中對齊呢? 默認值與該語法對齊。 這是代碼:
int main()
{
char *p, s[100];
long n;
int cmp,tesint;
char tes[50],tes1[50],tes2[50];
printf("tes : ");
fgets(tes,50,stdin);
strtok(tes,"\n");
printf("tesint : ");
scanf("%d",&tesint);
fflush(stdin);
printf("tes 1 : ");
fgets(tes1,50,stdin);
strtok(tes1,"\n");
printf("tes 2 : ");
fgets(tes2,50,stdin);
strtok(tes2,"\n");
printf("=======================================\n");
printf("= tes s : %25s =\n",tes);
printf("= tes int : %25d =\n",tesint);
printf("= tes 1 : %25s =\n",tes1);
printf("= tes 2 : %25s =\n",tes2);
printf("=======================================\n");
return 0;
}
當我運行它時:
tes : Opan Lazuardi Prayoga
tesint : 20
tes 1 : Laki-laki
tes 2 : Bogor, Jawa Barat
=======================================
= tes s : Opan Lazuardi Prayoga =
= tes int : 20 =
= tes 1 : Laki-laki =
= tes 2 : Bogor, Jawa Barat =
=======================================
Process returned 0 (0x0) execution time : 15.695 s
Press any key to continue.
這不是預期的輸出,我想這樣輸出:
tes : Opan Lazuardi Prayoga
tesint : 20
tes 1 : Laki-laki
tes 2 : Bogor, Jawa Barat
=======================================
= tes s : Opan Lazuardi Prayoga =
= tes int : 20 =
= tes 1 : Laki-laki =
= tes 2 : Bogor, Jawa Barat =
=======================================
或者像這樣:
tes : Opan Lazuardi Prayoga
tesint : 20
tes 1 : Laki-laki
tes 2 : Bogor, Jawa Barat
=======================================
= tes s : Opan Lazuardi Prayoga =
= tes int : 20 =
= tes 1 : Laki-laki =
= tes 2 : Bogor, Jawa Barat =
========================================
我知道我可以用 \t 或許多空格手動完成,但如果這樣做,從“tes s”到“test 2”的邊框“=”字符將不一樣。 我已經搜索了一個類似的問題,但我仍然不明白。 所以我要求在這里問清楚。 謝謝你。
在格式字符串中使用-
標志來告訴它左對齊。
printf("= tes s : %-25s =\n",tes);
printf("= tes int : %-25d =\n",tesint);
printf("= tes 1 : %-25s =\n",tes1);
printf("= tes 2 : %-25s =\n",tes2);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.