结构和其他数据形式

结构和其他数据形式

#include<stdio.h>
#include<string.h>
char *s_gets(char *st, int n);
#define MAXTITL 41
#define MAXAUTL 31
struct book
{
 	char title[MAXTITL];
 	char author[MAXAUTL];
	 float value;
 } ;
int main(void)
{
	  struct book library;
 	 printf("Please enter the book title.\n");
 	 s_gets(library.title, MAXTITL);
	  printf("Now enter the author.\n");
 	 s_gets(library.author, MAXAUTL);
 	 printf("Now enter the value.\n");
	  scanf("%f", &library.value);
  	printf("%s by %s: $%.2f\n", library.title, library.author, library.value);
 	 printf("%s: \" %s \" ($%.2f)\n", library.title, library.author, library.value);
 	 printf("Done.\n");
	  return 0;
}
 char *s_gets(char *st, int n)
 {
	  char *ret_val;
	  char *find;
 	 ret_val = fgets(st, n, stdin);
	  if(ret_val)
	  {
	   find = strchr(st, '\n');
	   if (find)
		    *find = '\0';
	   else
		    while(getchar() != '\n')
 			    continue;
 	 }
	  return ret_val;
 }
  1. 建立结构声明
    结构声明:描述了一个结构的组织布局。
struct book //book是可选的标记
{
	  char title[MAXTITL];
	  char author[MAXAUTL];
 	  float value;
 } ;

该声明描述了一个由两个字符数组和一个float类型变量组成的结构。
struct book dickens; 创建了一个结构变量dickens,该变量的结构布局是book。
2. 定义结构变量
结构的两层定义:①结构布局(告诉编译器如何表示数据,但是未让编译器为数据分配空间)
②结构变量:library,编译器为其分配空间

声明的简化

struct book
{
	char title[MAXTITL];
	char author[MAXAUTL];
	float value;
}library;

声明和定义组成一个步骤

struct{
	char title[MAXTITL];
	 char author[MAXAUTL];
 	float value;
}library;
`` 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值