#include <stdio.h>
#include <stdlib.h>
int main()
{
char firstname[15];
char lastname[15];
char crush_first[15];
char crush_last[15];
int babies;
printf("What is your first name?\n");
scanf("%s", firstname );
printf("What is your last name?\n");
scanf(" %s", lastname);
/* see i have added space before the character conversion but on exectution
of this file no space is in between the two strings*/
printf("What is your crush's first name?\n");
scanf("%s", crush_first );
printf("What is your crush's last name?\n");
scanf(" %s", crush_last );
printf("How many kids will you have?");
scanf("%d", &babies );
printf("%s%s will have a lovely marriage with %s%s and they will have %d kids",firstname,lastname,crush_first,crush_last,babies);
}
现在我要在默认情况下在字符串中添加空间。 “ __etc”我希望字符串也存储这些值。尽管我在%s之前反复添加了空格,但无法识别。
答案 0 :(得分:1)
来自scanf doc:
s matches a sequence of non-whitespace characters (a string) [...]
如果有人输入字符串的时间长于接收缓冲区,则会使缓冲区溢出。
如果您想读一行直到换行,也许使用fgets:
fgets(lastname, sizeof(lastname), stdin);