C[9] typedef 类型别名

C[9] typedef 类型别名

typedef是在计算机编程语言中用来为复杂的声明定义简单的别名。它与宏定义有些差异。它本身是一种存储类的关键字,与auto、extern、mutable、static、register等关键字不能出现在同一个表达式中。

示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <string.h>
//滔Roy  2021.11.05
//与Delphi 类型对应  2021.11.05
typedef long Integer;
typedef unsigned long Cardinal;
typedef signed char Shortint;
typedef int Smallint;
typedef unsigned char Byte;
typedef unsigned short Word;
typedef unsigned long Longword;
typedef long Longint;
 
 
typedef struct Ttest{
    int a1;
    char a2[100];   //定义大小 char a2[100];
    double a3;
}test;  //为结构取了类型别名,以下直接用别名引用,不用前缀struct
 
 
int main( )
{
 
    Integer a1=123456;   //自己定义的类型别名Integer
    printf( "Integer 类型别名的值 : %d\n", a1);
 
    Byte a2,a3;  //自己定义的类型别名 Byte
    a2=5,a3=10;
    printf( "a2 类型别名的值 : %d\n", a2);
    printf( "a3 类型别名的值 : %d\n", a3);
 
   test test1;     //结构别名引用,不用前缀struct  类型Ttest 的别名 test */
 
 
   /* test1 详述 */
   test1.a1=100;
   strcpy( test1.a2, "Hello TaoRoy1!");
   test1.a3=99.99;
 
   /* 输出 test1 信息 */
   printf( "test1.a1 : %d\n", test1.a1);
   printf( "test1.a2 : %s\n", test1.a2);
   printf( "test1.a3 : %4.2f\n", test1.a3);
 
 
   typedef char StrLine[100];   //
   StrLine aa="Hello StrLine!"//StrLine 类型代表了具有100个元素的字符数组
   printf( "StrLine的值 : %s\n", aa);
 
 
   return 0;
}

 

  

2、typedef 与 宏#define 的异同

  • #define 是 C 指令,用于为各种数据类型定义别名,与 typedef 类似。
  • typedef 仅限于为类型定义符号名称,#define 不仅可以为类型定义别名,也能为数值定义别名,比如您可以定义 1 为 ONE。
  • typedef 是由编译器执行解释的,#define 语句是由预编译器进行处理的。

 

 

 

创建时间:2021.11.05  更新时间:

posted on   滔Roy  阅读(156)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报

导航

点击右上角即可分享
微信分享提示