简体   繁体   English

C printf格式

[英]C printf formatting

I have the following function to print a list of books borrowers have taken out. 我具有以下功能来打印借款人已取出的书籍清单。

void searchBorrowerLoans(int a) {   
            int i;
            for (i = 1 ; i < MAX_BOOKS ; i++) {                 
                if (loanlog[i].borrowerid == a) {
                    NEWLINE;
                    printf("Borrower ID : %i <--Borrowing-> Book ID's : %i\n", loanlog[i].borrowerid, loanlog[i].bookid);
                }
            }
        }

I want to be able to format the results like this 我希望能够格式化这样的结果

 Borrower ID : 5 <---Borrowing---> Book ID's : 5, 3 , 2

Instead of (New line)- 代替(新行)-

 Borrower ID : 10 <--Borrowing-> Book ID's : 5

 Borrower ID : 10 <--Borrowing-> Book ID's : 7

 Borrower ID : 10 <--Borrowing-> Book ID's : 8

 Borrower ID : 10 <--Borrowing-> Book ID's : 9

I don't know what NEWLINE is, but it's not necessary to know, just do it like this 我不知道什么是NEWLINE ,但不必知道,只需这样做

printf("Borrower ID : %d <--Borrowing-> Book ID's :", a);
for (i = 0 ; i < MAX_BOOKS ; ++i)
 {
    if (loanlog[i].borrowerid == a)
        printf("%d, ", loanlog[i].bookid);
 }
printf("\n");

note that I fixed the loop to start from i = 0 because usualy in c that would be correct since arrays ire indexed from 0 not from 1 . 请注意,我将循环固定为从i = 0开始,因为在c中,这通常是正确的,因为数组的索引从0而不是从1

You will have to deal with an extra , at the end of the book id list. 你将不得不面对一个额外的,在书ID列表的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM