我有这个C代码:
#include "stdio.h"
main()
{
struct books
{
char name[100],author[100];
int year,copies;
}book1,book2;
printf("Enter details of first book\n");
gets(book1.name);
gets(book1.author);
scanf("%d%d",&book1.year,&book1.copies);
printf("Enter details for second book\n");
gets(book2.name);
gets(book2.author);
scanf("%d%d",&book2.year,&book2.copies);
printf("%s\n%s\n%d\n%d\n",book1.name,book1.author,book1.year,book1.copies);
printf("%s\n%s\n%d\n%d\n",book2.name,book2.author,book2.year,book2.copies);
}
这里发生的事情是它只扫描到第二本书的作者姓名。之后它直接打印输出。
这是我的input:(前两行是最初的 printf 语句)
Enter details of first book
warning: this program uses gets(), which is unsafe.
the c programmign laguagne
dfadsda
3432
23
Enter details for second book
ruby on rails
mark hammers
之后就直接打印输出:
the c programmign laguagne
dfadsda
3432
23
ruby on rails
0
0
这里有什么问题吗?我们还可以看到第二本书的名字被分配给作者。
我在用着gcc
作为 Mac OS X ML 上的编译器。