C语法简单测试

1、未初始化的枚举变量

 1 /* uninitialized-enum.c */
 2 #include <stdio.h>
 3 
 4 enum color{white = 1, black, blue};
 5 
 6 int main(void)
 7 {
 8     enum color bird;
 9     
10     printf("bird = %d
", bird);
11     
12     return 0;
13 }

输出结果:

$ ./uninitialized-enum
bird = 2130567168
$

2、十六进制数与switch语句测试

 1 /* test.cpp */
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 enum color{green, yellow, red, blue, black};
 7 
 8 int main(void)
 9 {
10     int i = 0x3e - 1;
11     
12     cout << "i = " << i << endl;
13     
14     color bird = blue;
15     switch(bird){
16         case green:
17         break;
18         case yellow:
19         break;
20         cout << "Hello, world!" << endl;
21         case red:
22         break;
23         cout << "Hello, world!" << endl;
24         case blue:
25         cout << "Yes, the color of this bird is blue!" << endl;
26         break;
27         default:
28         cout << "No, byebye!" << endl;
29         break;
30     }
31     
32     return 0;
33 }

输出结果:

$ ./test
i = 61
Yes, the color of this bird is blue!
$

3、sizeof关键字和处理器的字节序

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     char str[] = { 0x01, 0x00, 0x00, 0x00, 0x04, 0x01, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x10, 0x10 };
 6     unsigned ret, lid;
 7     
 8     ret = sizeof str;
 9     printf("ret = %d
", ret);
10     lid = *(unsigned *)(str+4);
11     printf("lid = %d
", lid);
12     
13     return 0;
14 }

备注:Intel Core处理器的字节序是小端序。

输出结果:

$ ./test
ret = 14
lid = 393476 (0x00, 0x06, 0x01, 0x04)
$

4、C语言中的整数

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     int i = 78, value;
 6     
 7     if((i << 2) == 0x0138)
 8         printf("YES!
");
 9     else
10         printf("NO!
");
11 
12     value = i + 0x0138;
13     printf("value = %d
", value);
14         
15     return 0;
16 }

输出结果:

$ ./test
YES!
value = 390
$

从输出结果可以看出,十进制数据与十六进制数据可以直接进行运算,而不需要经过转换。

另外,C语言中的整数有多种形式,二进制数、八进制数、十进制数、十六进制数、字符('A')都是整数,可以使用任何一种形式,或者在任何形式的整数之间进行整数运算。

5、循环结构、switch结构中的break语句

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     enum color {yellow, green, red, black};
 6     enum color bird = red;
 7     
 8     while(1){
 9 
10         switch(bird){
11         case yellow:
12             printf("the bird is yellow!
");
13             break;
14         case green:
15             printf("the bird is green!
");
16             break;
17         case red:
18             printf("the bird is red!
");
19             break;
20         default:
21             printf("the bird is black!
");
22             break;
23         }
24 
25         printf("use a break to get out of while!
");
26         break;
27     }
28     
29     printf("we are now at outside of while loop!
");
30     
31     return 0;
32 }

输出结果:

$ ./test
the bird is red!
use a break to get out of while!
we are now at outside of while loop!
$

从输出结果可以看出:switch中的break只对switch结构有用,对更外层的while循环无用;while循环中break语句可以跳出while循环。

因此,我们有如下结论:

1、switch结构并非循环结构,遇到break则不再执行该结构中剩下的语句;
2、break语句可以跳出当前的循环结构(一层)。

 

6、结构体的存储空间

 1 #include <stdio.h>
 2 
 3 typedef struct {
 4     unsigned            request;
 5     unsigned            len;
 6     unsigned char       data[0];
 7 }rms_message_t;
 8 
 9 int main(void)
10 {
11     printf("sizeof(unsigned) = %d
", sizeof(unsigned));
12     printf("sizeof(unsigned char) = %d
", sizeof(unsigned char));
13     printf("sizeof(rms_message_t) = %d
", sizeof(rms_message_t));
14     
15     return 0;
16 }

输出结果:

$ ./test
sizeof(unsigned) = 4
sizeof(unsigned char) = 1
sizeof(rms_message_t) = 8
$
原文地址:https://www.cnblogs.com/pyhou/p/7617665.html