[英]For some reason, printf function adds a space to what I want to display
我正在尝试在列中显示文本,但在输出的第二行添加了一个空格。 我做错了吗? (条目在字符串之前不包含空格)以下是涉及的功能:
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
放在格式字符串而不是字段中。 由于Mobile number
有13个字符,加上\\n
14和一个空格被添加为%-15s
填充,它会在\\n
之后,在下一行中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.