为了账号安全,请及时绑定邮箱和手机立即绑定

不换行可以运行,换行产生错误,为什么?

不换行可以运行,换行产生错误,为什么?

C
溯源1 2017-01-22 19:24:54
#include "stdio.h"#include<malloc.h>#include<string.h>#include<ctype.h>#define LEN struct studentstruct student{ char name[22]; int Chinese; int Math; int English; struct student*next;};struct student*create(){ char ch;    struct student *head;    struct student*p1,*p2;    int a=0;    printf("是否输入新数据:\n");        while(toupper(ch=getchar())=='Y')    {      p1=(LEN*)malloc(sizeof(LEN));      printf("name:");      scanf("%s",&(*p1).name);      printf("Chinese:");      scanf("%d",&(*p1).Chinese);      printf("Math:");      scanf("%d",&(*p1).Math);      printf("English:");      scanf("%d",&(*p1).English);      getchar();//getchar吸收回车      printf("是否输入新数据:\n");      a++;      if(a==1)       head=p1;      else        (*p2).next=p1;      p2=p1;      p1->next=NULL;      }    return(head);}void print(struct student *head){ struct student *p; p=head; while(p!=NULL) { printf("\nname:%-8sChinese:%-5dMath:%-5 dEnglish:%-5d",p->name,p->Chinese,p->Math,p->English);//只是一个名 p=p->next;//测试随便输 }}void main( ){ //struct student *stu; //stu=create(); print(create());}
查看完整描述

5 回答

已采纳
?
Plan_C

TA贡献5条经验 获得超18个赞

字符串被截断了,引号不能跨行,可以这样 写

printf("123"

"456"

"789");

这样 "123456789"是一个字符串

查看完整回答
反对 回复 2017-01-24
?
习惯受伤

TA贡献885条经验 获得超1144个赞

因为换行之后,就不是一个完整的字符串了,破坏了语法,所以会出错。如果你只想输出换行,请使用\n转义符。
查看完整回答
1 反对 回复 2017-01-22
?
慕后端2529822

TA贡献6条经验 获得超3个赞

你可以 拼接起来

查看完整回答
反对 回复 2017-02-09
?
_dark

TA贡献18条经验 获得超6个赞

因为编辑器把换行认为成结束了,你可以在第一行最后面加个"\",说明这一行和下一行是接着的

查看完整回答
反对 回复 2017-01-23
?
BanJim3313278

TA贡献7条经验 获得超0个赞

%-5d 是格式输出,是一个整体,不能分割

查看完整回答
反对 回复 2017-01-23
  • 5 回答
  • 0 关注
  • 1730 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信