C语言进阶课程学习记录-第11课 - enum, sizeof, typedef 分析

C语言进阶课程学习记录-第11课 - enum, sizeof, typedef 分析


本文学习自狄泰软件学院 唐佐林老师的 C语言进阶课程,图片全部来源于课程PPT,仅用于个人学习记录

枚举的用法

枚举类型
特殊意义

实验-enum

#include <stdio.h>

enum
{
    ARRAY_SIZE = 10
};

enum Color
{
    RED    = 0x00FF0000,
    GREEN  = 0x0000FF00,
    BLUE   = 0x000000FF
};

void PrintColor(enum Color c)
{
    switch( c )
    {
        case RED:
            printf("Color: RED (0x%08X)\n", c);
            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[])
{
    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++)
    {
        printf("%d\n", array[i]);
    }
}


int main()
{
    enum Color c = GREEN;

    int array[ARRAY_SIZE] = {0};

    PrintColor(c);

    InitArray(array);

    PrintArray(array);

    return 0;
}

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

*/

sizeof用法

sizeof
sizeof2

实验-sizeof

#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);

    size = sizeof(f());

    printf("size = %d\n", size);

    return 0;
}
/*output:
var = 0, size = 4
size = 4

*/

typedef

typedef1
typedef

实验-typedef

#include <stdio.h>

typedef int Int32;

struct _tag_point
{
    int x;
    int y;
};

typedef struct _tag_point Point;

typedef struct
{
    int length;
    int array[];
} SoftArray;

typedef struct _tag_list_node 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;
}
/*output:

*/

小结

enum用于定义离散值类型
enum定义的值是真正意义.上的常量sizeof是编译器的内置指示符
sizeof不参与程序的执行过程
typedef用于给类型重命名
重名的类型可以在 typedef语句之后定义

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值