[关闭]
@fiy-fish 2015-06-29T12:27:19.000000Z 字数 516 阅读 3418

scanf函数在使用逗号做分隔符的一点困惑

C


代码如下:

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. typedef struct student{
  4. int snum;
  5. char name[20];
  6. int sex;
  7. char major[30];
  8. long num;
  9. }STU;
  10. void add(STU *p1)
  11. {
  12. printf("请输入学生的学号,姓名,性别,专业,电话\n");
  13. scanf("%d %s %d %s %ld",&p1->snum,p1->name,&p1->sex,p1->major,&p1->num);
  14. // 当scanf函数中得空格换成是逗号的时候,程序的运行结果会出错……
  15. // scanf("%d,%s,%d,%s,%ld",&p1->snum,p1->name,&p1->sex,p1->major,&p1->num);
  16. }
  17. int main()
  18. {
  19. STU stu[10];
  20. STU *p=NULL;
  21. p = stu;
  22. add(p);
  23. fflush(stdin);
  24. printf("%d,%s,%d,%s,%ld\n",p->snum,p->name,p->sex,p->major,p->num);
  25. return 0;
  26. }

有逗号的时候运行的结果如下图:
erro
erro1

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注