enum, sizeof, typedef

枚举类型enum

  • enum 是 C 语言中的一种自定义类型
  • enum 值是可以根据需要自定义的整型值
  • 第一个定义的 enum 值默认为 0
  • 默认情况下的 enum 值在前一个定义值得基础上加 1
  • enum 类型的变量只能取定义时得离散值
复制代码
  void code()
  {
      enum Color
      {
          GREEN,      // 0
          RED = 2,    // 2
          BLUE,       // 3
      };
      
      enum Color c = GREEN;
      
      printf("%d\n", c);
  }

枚举类型的特殊意义

  • enum 中定义的值是 C 语言中真正意义上的常量
  • 在工程中 enum 多用于定义整形常量或离散的整型常量
复制代码
void code()
{
    enum                    // 无名枚举,用于定义常量
    {
        ARRAY_SIZE = 10,    // 定义数组大小
    };
    
    int array[ARRAY_SIZE] = {0};
    int i = 0;
    
    for(i=0; i< ARRAY_SIZE; i++)
    {
        array[i] = i + 1;
    }
}

实例分析 : enum 的使用

复制代码
#include <stdio.h>

enum
{
    ARRAY_SIZE = 10,        // 定义常量
};

enum Color
{
    RED    = 0x00FF0000,    // 定义整形离散值
    GREEN  = 0x0000FF00,
    BLUE   = 0x000000FF
};

void PrintColor(enum Color c)
{
    switch( c )            // 用于 switch case
    {
        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;
        
        default:
        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);
}

输出:

输出:

Color: GREEN (0x0000ff00)

1

2

3

4

5

6

7

8

9

10

sizeof 关键字

  • sizeof 是编译器的内置指示符

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

  • sizeof 的值在编译期就以确定

    • sizeof 用于类型 : sizeof(type)

    • sizeof 用于变量 : sizeof(var) 或 sizeof var

      void code()
      {
      int var = 0;

      复制代码
      printf("%d\n", sizeof(int));
      printf("%d\n", sizeof(var));
      printf("%d\n", sizeof var);

      }

实例:

复制代码
#include <stdio.h>

double f()
{
    printf("D.T.Software\n");
    
    return 0.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);
}

输出:

复制代码
输出:
var = 0, size = 4
size = 8

分析: 注意 var = 0 ; sizeof(f()) = 8
sizeof 不是函数,不会在运行时计算变量或类型的值,而是在编译时,所有的 sizeof 都被具体的值替换。
sizeof(var++) 在编译时直接被替换,var++ 得不到执行。

typedef

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

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

  • typedef 重命名的类型:

    • 可以在 typedef 语句之后定义
    • 不能被 unsigned 和 signed 修饰

实例:

复制代码
#include <stdio.h>
// --- 定义方式 1 
typedef int Int32;

// --- 定义方式 2 
struct _tag_point
{
    int x;
    int y;
};
typedef struct _tag_point Point;

// --- 定义方式 3 
typedef struct
{
    int lengyh;
    int array[];
}SoftArray;

// --- 定义方式 14 
// 编译器没有要求被重命名的类型必须先定义在可以
typedef struct _tag_list_node ListNode;
struct _tag_list_node
{
    ListNode* next;
};


int main()
{
    Int32 i = -100;                // int
    // unsigned Int32 ii = 0;   // Error
    Point p;                    // struct _tag_point
    SoftArray* sa = NULL;
    ListNode* node = NULL;      // struct _tag_list_node
}

小结

  • enum 用于定义离散值类型

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

  • sizeof 是编译器的内置指示符

  • sizeof 不参与程序的执行过程

  • typedef 用于给类型重命名

    • 重命名的类型可以在 typedef 语句之后
相关推荐
blasit2 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_3 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星3 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛5 天前
delete又未完全delete
c++
端平入洛6 天前
auto有时不auto
c++
哇哈哈20216 天前
信号量和信号
linux·c++
多恩Stone6 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马6 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝6 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc7 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法