[英]sprintf() and printf() printing multiple lines in console?
我有以下代碼:
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: %.2f\n",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
字符都將被打印出來。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.