11-enum,sizeof,typedef分析

注:博客中内容主要来自《狄泰软件学院》,博客仅当私人笔记使用。

测试环境:Ubuntu 10.10

GCC版本:4.4.5

 

一、枚举类型的使用方法

1)enum是C语言中的一种自定义类型

2)enum值是可以根据需要自定义的整型值

3)第一个定义的enum值默认为0

4)默认情况下的enum值是在前一个定义值的基础上加1

5)enum类型的变量只能取定义时的离散值

enum Color
{
    GREEN,
    RED = 2,
    BLUE    //实际为3 
};
​
enum Color c = GREEN;
printf("%d\n", c);
除了用自身成员赋值,还可以用其它数值赋值。

 

二、枚举类型的特殊意义

1)enum中定义的值是C语言中真正意义上的常量

2)在工程中enum多用于定义整型常量

enum                //无名枚举,用于定义常量
{
    ARRAY_SIZE = 10,    //定义数组大小
};
​
int array[ARRAY_SIZE] = {0};    //通过编译,证明是常量
int i = 0;

for(i=0; i<ARRAY_SIZE; i++)
{
   array[i] = i + 1;
}

 

实例分析
enum的使用
11-1.c
#include <stdio.h>
​
enum        //定义一个整型常量10
{
    ARRAY_SIZE = 10    //定义数组大小
};
​
enum Color      //定义一个枚举类型颜色
{
    RED    = 0x00FF0000,
    GREEN  = 0x0000FF00,
    BLUE   = 0x000000FF
};
​
void PrintColor(enum Color c)       //枚举类型参数,参数名为C
{
    switch( c )
    {
        case RED:
            printf("Color: RED (0x%08X)\n", c);     //0x:16进制字头0x,普通字符  %08X:8位16进制类型数据
            break;
        case GREEN:
            printf("Color: GREEN (0x%08X)\n", c);
            break;
        case BLUE:
            printf("Color: BLUE(0x%08X)\n", c);
            break;
    }
}
​
void InitArray(int array[])     //初始化一个长度为10的数组并付初始值为1-10
{
    int i = 0;
    
    for(i=0; i<ARRAY_SIZE; i++) //
    {
        array[i] = i + 1;
    }
}
​
void PrintArray(int array[])    //打印数组数值
{
    int i = 0;
    
    for(i=0; i<ARRAY_SIZE; i++)     //用ARRAY_SIZE固定了数组长度
    {
        printf("%d\n", array[i]);
    }
}
​
​
int main()
{
    enum Color c = GREEN;           //定义一个枚举类型变量C,C初始化为GREEN
    
    int array[ARRAY_SIZE] = {0};    //初始化长度为10的整型数组
    
    PrintColor(c);                  //打印绿色及其地址
    
    InitArray(array);               //初始化数组数值1-10
    
    PrintArray(array);              //打印数组
    
    return 0;
}

操作:

1) gcc 11-1.c -o 11-1.out编译正确,打印结果:

Color: GREEN(0x0000FF00)
1    
2    
3    
4    
5    
6    
7    
8    
9    
10    

 

三、sizeof关键字的用法

1)sizeof是编译器的内置指示符

2)sizeof用于计算类型或变量所占的内存大小

3)sizeof的值在编译期就已经确定

 

 

四、为sizeof关键字正名

1)sizeof是C语言的内置关键字而不是函数

 - 在编译过程中所有的sizeof将被具体的数值所替换

 - 程序的执行过程与sizeof没有任何关系

 

下面的程序输出什么?

int var = 0;
int size = sizeof(var++);
printf("var = %d, size = %d\n", var, size);

输出结果 var = 0, size = 4

分析:sizeof计算的值其实在编译时就已经确定了,在实际运行时var++就不执行了。

 

 

编程实验
sizeof的本质
11-2.c
#include <stdio.h>
​
int f()
{
    printf("Delphi Tang\n");
    
    return 0;
}
​
int main()
{
    int var = 0;
    
    int size = sizeof(var++);
    
    printf("var = %d, size = %d\n", var, size);    //0,4
    
    size = sizeof(f());     //sizeof在编译时就已经被替换了,因此运行时为固定数值
    
    printf("size = %d\n", size);    //4
    
    return 0;
}

操作:

1) gcc 11-2.c -o 11-2.out编译正确,打印结果:

var = 0, size = 4
size = 4

分析:

        用sizeof()计算函数大小,实质计算的是函数类型大小。

 

五、typedef的意义

面试中......

考官:你能说说typedef具体的意义吗?

应聘者:typedef用于定义一种新的类型...

 

1)typedef用于给一个已经存在的数据类型重命名

2)typedef本质上不能产生新的类型

3)typedef重命名的类型:

  - 可以在typedef语句之后定义

  - 不能被unsignedsigned修饰

用法:

typedef type new_name;

 

 

实例分析
typedef使用示例
11-3.c
#include <stdio.h>
​
typedef int Int32;    //int的新名字Int32
​
struct _tag_point
{
    int x;
    int y;
};
​
typedef struct _tag_point Point;    //新名字Point
​
typedef struct
{
    int length;
    int array[];
} SoftArray;     //新名字SoftArray
​
typedef struct _tag_list_node ListNode;    //新名字ListNode
struct _tag_list_node
{
    ListNode* next;
};
​
int main()
{
    Int32 i = -100;        // int 
    //unsigned Int32 ii = 0;
    Point p;               // struct _tag_point
    SoftArray* sa = NULL;   
    ListNode* node = NULL; // struct _tag_list_node*
    
    return 0;
}
​操作:
1) gcc 11-3.c -o 11-3.out编译正确,打印无。

 

小结

1)enum用于定义离散值类型

2)enum定义的值是真正意义上的常量

3)sizeof是编译器的内置指示符

4)sizeof不参与程序的执行过程

5)typedef用于给类型重命名

     - 重名的类型可以在typedef语句之后定义

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值