asdasddd 发表于 2014-12-15 22:50:53

scanf函数一起输入和分开输入有什么区别

我现在在学习结构体,刚刚写程序的时候碰到一个奇怪的地方
scanf("%d %f",&p1->num,&p1->score);
printf("%d %f\n",p1->num,p1->score);
这样输出的结果不是预期的!
scanf("%d",&p1->num);
scanf("%f",&p1->score);
printf("%d %f\n",p1->num,p1->score);

而这样输出的结果和预期的吻合!
请问这两种scanf的输入方法区别在哪?



这是整个程序的代码
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

#define LEN sizeof(struct student)//student结构的大小

struct student
{
      long num;
      float score;
      struct student *next;
};



void main()
{
      int n;
      struct student *head,*p1,*p2,*p;

      p1=p2=(struct student *)malloc(LEN);
      head=NULL;
      n=0;
      scanf("%ld,%f",&p1->num,&p1->score);
      for(;p1->num!=0;)
      {
            n=n+1;
            if(n==1)
            {
                  head=p1;
                  p2=p1;
                  p1=(struct student *)malloc(LEN);
                  scanf("%ld,%f",&p1->num,&p1->score);
            }
            else
            {
                  p2->next=p1;
                  p2=p1;
                  p1=(struct student *)malloc(LEN);
                  scanf("%ld,%f",&p1->num,&p1->score);
            }
      }
      p2->next=NULL;

      p=head;
      if(head)
      {
            do
            {
                  printf("%ld %f\n",p->num,p->score);
                  p=p->next;
            }while(p);
      }

}


~风介~ 发表于 2014-12-15 23:17:59

应该是一样的!
scanf("%d %f",&p1->num,&p1->score); 输入两个数要用空格隔开哦;
scanf("%d,%f",&p1->num,&p1->score); 输入两个数要用分号隔开哦;

BeatificDevin 发表于 2014-12-15 23:20:02

假如:scanf("%d %d",&a,&b);你在输入数据的时候要用空格隔开,像3 4这样。
scanf("%d,%d",&a,&b)这样的话用逗号隔开。如3,4
分开写的scanf函数,是一个数字一个数字的输入

风之残月 发表于 2014-12-16 09:24:51

没区别,除了输入方式不同
scanf("%d %f",&p1->num,&p1->score);需要输入两个用空格隔开的数据

scanf("%d",&p1->num);输入一个数据后回车
scanf("%f",&p1->score);输入一个数据后回车

丶浅唱 发表于 2014-12-16 18:57:22

中间要用空格隔开、、、、

asdasddd 发表于 2014-12-16 20:11:00

看了你们的回复之后,我又试了下,确实是一样的!
可能我昨天写漏了什么,出错了!

ganlei00 发表于 2014-12-18 18:21:42

extern 哈哈哈 啊啊啊啊啊
页: [1]
查看完整版本: scanf函数一起输入和分开输入有什么区别