【发布时间】:2013-10-22 22:53:21
【问题描述】:
我有这个 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);
}
这里发生的是它只扫描到第二本书的作者姓名。之后,它直接打印输出。
这是我的输入:(前两行是最初的 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
这里有什么问题?我们还可以看到,第二本书的名称被分配给了作者。
我在 Mac OS X ML 上使用 gcc 作为编译器。
【问题讨论】:
标签: c structure scanf fgets gets