c-sprintf()和printf()在控制台中打印多行



我有以下代码:

void printStudent(Person per) {
  char string[100];
  char firstName[10];
  strcpy(firstName,per.firstName);
  char familyName[20];
  strcpy(familyName,per.familyName);
  char teleNum[10];
  strcpy(teleNum,per.teleNum);
  int gpa = per.StuEmp.stu.gpa;
  int numCourses = per.StuEmp.stu.numCourses;
  float tuFees = per.StuEmp.stu.tuFees;
  sprintf(string, "n%s %s Tel: %s, GPA: %d, Courses: %d, Tuition: %.2fn",firstName,familyName,teleNum,gpa,numCourses,tuFees);
  printf("%s",string);
} 

我希望字符串在控制台中打印在一行,但它以以下格式打印:

Bob
 Joe
 Tel: 123456
, GPA: 8, Courses: 6, Tuition: 12345.89

我想这样打印:

Bob Joe                              Tel: 123456, GPA: 8, Courses: 6,Tuition: 12345.89

per中的字符串可能以n结尾,因此正在复制n字符。当您打印出最后一个字符串时,所有这些n字符都将被打印出来。

最新更新