C 出于某种原因,printf函数为我想要显示的内容添加了一个空格
我试图在列中显示文本,但在输出的第二行添加了一个空格。我做错什么了吗?(条目在字符串前不包含空格)以下是所涉及的函数:C 出于某种原因,printf函数为我想要显示的内容添加了一个空格,c,printf,C,Printf,我试图在列中显示文本,但在输出的第二行添加了一个空格。我做错什么了吗?(条目在字符串前不包含空格)以下是所涉及的函数: add(List *book) { Contact *runner = book->una; Contact *node = (Contact *) malloc(sizeof(Contact)); if (node == NULL); else { printf("Add a contact\n"); printf("Enter the
add(List *book)
{
Contact *runner = book->una;
Contact *node = (Contact *) malloc(sizeof(Contact));
if (node == NULL);
else {
printf("Add a contact\n");
printf("Enter the first name: ");
scanf("%s", node->fname);
printf("Enter the last name: ");
scanf("%s", node->lname);
printf("Enter the mobile number: ");
scanf("%s", node->number);
printf("\nContact added!\n\n");
node -> next = NULL;
if(book->una == NULL){
book->una = node;
}
else
{
while( runner->next != NULL)
{
runner = runner->next;
}
runner->next = node;
}
}
}
display(List *book)
{
Contact *runner = book -> una;
if(runner == NULL)
{
printf("%20s", "PHONEBOOK EMPTY!");
return;
}
printf("Contact list\n");
printf("%-15s%-15s%-15s", "First Name", "Last Name", "Mobile number\n");
while( runner != NULL)
{
printf("%-15s%-15s%-15s\n", runner->fname, runner->lname, runner->number);
runner = runner->next;
}
}
/*An example output basically turns out like this:
First Name Last Name Mobile Number
James Harrison 123456
Wendy Barnes 00000
Cam Rodriguez 575938*/
任何输入数据都将如上所示进行打印。您应该将
\n
放在格式字符串中,而不是字段中。由于手机号码
有13个字符,加上\n
14个字符,并且在%-15s
中添加了一个空格作为填充,因此它位于\n
之后,因此在下一行。问题似乎不在这里。请更新您的问题,并提供问题的详细信息。