我是C 语言的新手,处理对我来说有点挑战性的文本格式。我尝试输入缩写JS(字符之间的空格)并命名John Smith,我的缩写数组存储不应该的值JSJohn Smith 。
有人可以解释我为什么会这样。
这是我的代码片段(只是示例)
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
struct Person
{
char initials[5];
char name[35];
};
int main(void)
{
struct Person person;
printf("Please enter symbols: ");
scanf("%6[^\n]%*c", person.initials);
printf("%s\n", person.initials);
printf("Please enter your name: ");
scanf("%35[^\n]%*c", person.name);
printf("%s\n", person.name);
printf("%s\n", person.initials);
}
输出 :
Please enter symbols: J. S.
J. S.
Please enter your name: John Smith
John Smith
J. S.John Smith
将非常感谢您的帮助。