zoukankan      html  css  js  c++  java
  • C语言进阶——enum, sizeof, typedef 分析11

    枚举类型的使用方法:

    • enum是C语言的 一种自定义类型
    • enum值是可以根据需要自定义的整型值
    • 第一个enum的值默认为0
    • 默认情况下的enum值是在前一个定义值的基础上加 1
    • enum类型的变量只能去定义时的离散值
     1 enum Color
     2 {
     3     GREEN,
     4     BLUE,
     5     RED
     6 };
     7 
     8 enum Color c = GREEN;
     9 
    10 printf("%d
    ",c);

    枚举类型的特殊意义:

    • enum中定义的值是C语言真正意义上的常量
    • 在工程中enum多用于定义整型常量
     1 enum   //无名枚举,用于定义常量
     2 {
     3     ARRAY_SIZE =10,   //定义数组的大小
     4 };
     5 
     6 int array[ARRAY_SIZE] = {0};
     7 int i=0;
     8 for(i=0;i<ARRAY_SIZE;i++)
     9 {
    10     ayyay[i]=i+1;
    11 }

    示例分析:

     1 #include <stdio.h>
     2 
     3 enum
     4 {
     5     ARRAY_SIZE = 10
     6 };
     7 
     8 enum Color
     9 {
    10     RED    = 0x00FF0000,
    11     GREEN  = 0x0000FF00,
    12     BLUE   = 0x000000FF
    13 };
    14 
    15 void PrintColor(enum Color c)
    16 {
    17     switch( c )
    18     {
    19         case RED:
    20             printf("Color: RED (0x%08X)
    ", c);
    21             break;
    22         case GREEN:
    23             printf("Color: GREEN (0x%08X)
    ", c);
    24             break;
    25         case BLUE:
    26             printf("Color: BLUE(0x%08X)
    ", c);
    27             break;
    28     }
    29 }
    30 
    31 void InitArray(int array[])
    32 {
    33     int i = 0;
    34     
    35     for(i=0; i<ARRAY_SIZE; i++)
    36     {
    37         array[i] = i + 1;
    38     }
    39 }
    40 
    41 void PrintArray(int array[])
    42 {
    43     int i = 0;
    44     
    45     for(i=0; i<ARRAY_SIZE; i++)
    46     {
    47         printf("%d
    ", array[i]);
    48     }
    49 }
    50 
    51 
    52 int main()
    53 {
    54     enum Color c = GREEN;
    55     
    56     int array[ARRAY_SIZE] = {0};
    57     
    58     PrintColor(c);
    59     
    60     InitArray(array);
    61     
    62     PrintArray(array);
    63     
    64     return 0;
    65 }

    sizeof 关键字的用法:

    • sizeof 是编译器的内置指示符
    • sizeof 用于计算类型或变量所占的内存的大小
    • sizeof 的值在编译期就已经确定

    sizeof 用于类型:

      sizeof(type)

    sizeof 用于变量:

      sizeof(var) 或 sizeof  var    --》这种不带括号的用法可以充分说明sizeof不是一个函数

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

    • 在编译过程中所有的 sizeof 将被具体的数值所替换
    • 程序的执行过程与 sizeof 没有任何关系

    思考下面的程序会输出什么?

    int var =0;

    int size = sizeof(var++);

    printf("var = %d ,size = %d ",var,size);

    示例:

     1 #include <stdio.h>
     2 
     3 int f()
     4 {
     5     printf("Delphi Tang
    ");
     6     
     7     return 0;
     8 }
     9 
    10 int main()
    11 {
    12     int var = 0;
    13     
    14     int size = sizeof(var++);
    15     
    16     printf("var = %d, size = %d
    ", var, size);
    17     
    18     size = sizeof(f());
    19     
    20     printf("size = %d
    ", size);
    21     
    22     return 0;
    23 }

    输出结果可能有点意外,因为sizeof在编译期间就被替换了,所以不会参与到运行期间

    下面模拟一个场景

    面试中。。。。。

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

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

    考官:你回家种田吧!

    typedef 的意义

    • typedef 用于给一个已经存在的数据类型重命名
    • typedef 本质上不能产生新的类型
    • typedef重命名的类型  1. 可以在typedef 语句后定义  2. 不能被unsigned 和 signed 修饰
    • 用法     typedef type  new_name

    示例:

     1 #include <stdio.h>
     2 
     3 typedef int Int32;
     4 
     5 struct _tag_point
     6 {
     7     int x;
     8     int y;
     9 };
    10 
    11 typedef struct _tag_point Point;
    12 
    13 typedef struct
    14 {
    15     int length;
    16     int array[];
    17 } SoftArray; 
    18 
    19 typedef struct _tag_list_node ListNode;
    20 struct _tag_list_node
    21 {
    22     ListNode* next;
    23 };
    24 
    25 int main()
    26 {
    27     Int32 i = -100;        // int 
    28     //unsigned Int32 ii = 0;
    29     Point p;               // struct _tag_point
    30     SoftArray* sa = NULL;   
    31     ListNode* node = NULL; // struct _tag_list_node*
    32     
    33     return 0;
    34 }

    小结:

    • enum 用于丁意思离散值的类型
    • enum 定义的值是真正意义上的常量
    • sizeof 是编译器的内置指示符
    • sizeof 不参与程序的执行过程
    • typedef 用于给类型重命名,重命名的类型可以在 typedef 语句之后定义
  • 相关阅读:
    js 数据结构-栈与队列
    mysql (已解决)Access denied for user 'root'@'localhost' (using password: NO)
    mysql (已解决p)MYSQL5.7启动不了,本地计算机上的 MySQL57 服务启动后停止。
    mysql 慢查询日志,灾难日志恢复,错误日志
    php json的相关操作
    (转)LitJson 遍历key
    (转)用Eclipse进行C++开发时Bianry not found的问题解决
    (转) 在Eclipse中进行C/C++开发的配置方法(20140721最新版)
    (转)如何在eclipse的配置文件里指定jdk路径
    (转)mongodb分片
  • 原文地址:https://www.cnblogs.com/luojianyi/p/9374478.html
Copyright © 2011-2022 走看看