C[9] typedef 类型别名
C[9] typedef 类型别名
typedef是在计算机编程语言中用来为复杂的声明定义简单的别名。它与宏定义有些差异。它本身是一种存储类的关键字,与auto、extern、mutable、static、register等关键字不能出现在同一个表达式中。
示例:
#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 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!
浙公网安备 33010602011771号