鱼C论坛

 找回密码
 立即注册
查看: 4059|回复: 2

为什么用 sacnf("%5.1f"'&p1->score) 就不行?

[复制链接]
发表于 2011-8-12 19:51:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<stdlib.h>
  4. #define NULL ((void *)0)
  5. #define LEN sizeof(struct student)
  6. struct student
  7. {
  8. long num;
  9. float score;
  10. struct student *next;
  11. };
  12. int n;
  13. struct student *creat(void)
  14. {
  15. struct student *head;
  16. struct student *p1,*p2;
  17. n=0;
  18. p1=p2=(struct student*)malloc(LEN); //开辟新的空间并把空间的首地址给p1p2

  19. printf("input a num: ");
  20. scanf("%ld",&p1->num);

  21. printf("input a score: ");
  22. scanf("%5.1f",&p1->score);    //如果这里写scanf("%f",&p1->score);输出就没有问题.

  23. head=NULL; //头指针为空
  24. while(p1->num)
  25. {
  26. n=n+1;
  27. if(n==1)
  28. head=p1;
  29. else
  30. {
  31. p2->next=p1;
  32. }
  33. p2=p1;
  34. p1=(struct student*)malloc(LEN);

  35. printf("\ninput a num: "); //开辟新空间
  36. scanf("%ld", &p1->num);

  37. printf("input a score: ");
  38. scanf("%5.1f", &p1->score); //如果这里写scanf("%f",&p1->score);输出就没有问题.
  39. }
  40. p2->next=NULL;
  41. return(head);
  42. }
  43. void print(struct student *head)
  44. {
  45. struct student *p;
  46. printf("\nNow,These %d records are:\n\n",n);
  47. p=head;
  48. if(head!=NULL)
  49. do
  50. {
  51. printf("this num %ld is score%5.1f:\n",p->num,p->score);  

  52. p=p->next;
  53. }
  54. while(p!=NULL);
  55. }
  56. main()
  57. {
  58. struct student *head;

  59. head=creat();
  60. print(head);
  61. system("pause");

  62. }
复制代码

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-12 21:22:40 | 显示全部楼层
%f,不要用5.1地, scanf和printf的格式化字符串是不一样的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-8-13 09:46:27 | 显示全部楼层
scanf("%5.1f", &p1->score); 这个5.1可以用在printf中 scanf没有这种规定
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 18:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表