C(四)

  1. union
#include <stdio.h>
#include <string.h>
union Data
{
   int i;
   float f;
   char  str[20];
};
 
int main( )
{
   union Data data;        
   printf( "Memory size occupied by data : %d\n", sizeof(data)); // 20
   data.i = 10;
   printf("data.i : %d\n", data.i);
   data.f = 220.5;
   printf("data.f : %f\n", data.f);
   strcpy(data.str, "C Programming");
   printf("data.str : %s\n", data.str);
   return 0;
}
  1. typedef
#include<stdio.h>
#include<string.h>
typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book; 
int main( )
{
   Book book;
   strcpy( book.title, "C 教程");
   strcpy( book.author, "Runoob"); 
   strcpy( book.subject, "编程语言");
   book.book_id = 12345;
   getchar();
}
  1. gets(), puts()
#include <stdio.h>
int main( )
{
   char str[100];
   printf( "Enter a value :");
   gets(str);
 
   printf( "\nYou entered: ");
   puts(str);
   printf("haha");
   getchar();
}   
  1. ANSI C预定义宏
printf("%s %s\n", __DATE__, __TIME__); // 打印当前日期、时间
#if !defined(MESSAGE)
    #define MESSAGE "hello"
#endif

int main(void)
{
   printf("Here is the message: %s\n", MESSAGE);  
   getchar();
}   

// 参数化的宏
#define MAX(x,y) x > y ? x : y
// #define MAX(x,y) ((x) > (y) ? (x) : (y))
int main(void)
{
   printf("Max between 20 and 10 is %d\n", MAX(10, 20));  
   getchar();
   return 0;
}
#ifndef HEADER_FILE
#define HEADER_FILE
the entire header file file

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值