C语言进阶——enum, sizeof, typedef 分析11

时间:2018-07-26 21:55:10   收藏:0   阅读:200

 

枚举类型的使用方法:

 1 enum Color
 2 {
 3     GREEN,
 4     BLUE,
 5     RED
 6 };
 7 
 8 enum Color c = GREEN;
 9 
10 printf("%d\n",c);

枚举类型的特殊意义:

 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)\n", c);
21             break;
22         case GREEN:
23             printf("Color: GREEN (0x%08X)\n", c);
24             break;
25         case BLUE:
26             printf("Color: BLUE(0x%08X)\n", 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\n", 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(type)

sizeof 用于变量:

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

 

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

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

int var =0;

int size = sizeof(var++);

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

 

示例:

 1 #include <stdio.h>
 2 
 3 int f()
 4 {
 5     printf("Delphi Tang\n");
 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\n", var, size);
17     
18     size = sizeof(f());
19     
20     printf("size = %d\n", size);
21     
22     return 0;
23 }

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

 

下面模拟一个场景

面试中。。。。。

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

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

考官:你回家种田吧!

 

typedef 的意义

示例:

 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 }

 

小结:

原文:https://www.cnblogs.com/luojianyi/p/9374478.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!