数据类型基础回顾
大家好,最近在面试,被打击了,好多基础都忘记了,做了些实验,希望对大家有所帮助。
#include <stdio.h>
typedef enum {
K1,
K2
}exa_enum;
int main(int argc, char **argv) {
exa_enum hello = 123;
printf("*********type define specifier size test start************ \n\n");
printf( \
"\tsizeof(int):%d bytes\n"
"\tsizeof(char):%d bytes\n"
"\tsizeof(long int):%d bytes\n"
"\tsizeof(short int):%d bytes\n"
"\tsizeof(long long int):%d bytes\n"
"\tsizeof(long double):%d bytes\n"
"\n", \
sizeof(int), \
sizeof(char), \
sizeof(long int) , \
sizeof(short int) , \
sizeof(long long int) , \
sizeof(long double) \
);
printf("*********constant pointer and pointer constant test start************ \n\n");
char *str1 = "hello World";
// char const *ptr1 = str1; // you can also write constant pointer like this
const char *ptr1 = str1; // constant pointer
char *str2 = "hello";
char * const ptr2 = str2; // pointer constant
// you can't do the following;
// ptr2 = str1;
// *ptr1 = 'A';
// you can do the following
ptr1 = str2;
str2 = "fuck you!";
printf("\t ptr1:%s;ptr2:%s\n\n", ptr1, ptr2);
printf("*********enum size test start************ \n\n");
printf("\tsizeof(exa_enum):%d bytes\n\tsizeof(hello):%d bytes\n", sizeof(exa_enum), sizeof(hello));
unsigned int array_t;
printf("\tarray size %8x , enum value %d\n", array_t, hello);
return 0;
}
====================================================================================
x86运行结果如下:
PS:编译器GCC
*********type define specifier size test start************
sizeof(int):4 bytes
sizeof(char):1 bytes
sizeof(long int):4 bytes
sizeof(short int):2 bytes
sizeof(long long int):8 bytes
sizeof(long double):12 bytes
*********constant pointer and pointer constant test start************
ptr1:hello;ptr2:hello
*********enum size test start************
sizeof(exa_enum):4 bytes
sizeof(hello):4 bytes
array size bfefbcc8 , enum value 123
====================================================================================
ARM运行结果如下:
PS:编译器,交叉编译工具
*********type define specifier size test start************
sizeof(int):4 bytes
sizeof(char):1 bytes
sizeof(long int):4 bytes
sizeof(short int):2 bytes
sizeof(long long int):8 bytes
sizeof(long double):8 bytes
*********constant pointer and pointer constant test start************
ptr1:hello;ptr2:hello
*********enum size test start************
sizeof(exa_enum):4 bytes
sizeof(hello):4 bytes
array size beb00cf4 , enum value 123
思维定势的影响,我以为array,取数组名取得就是数组的size,记得当时做题目时蛮自信的,等回来一运行...悲剧啦 我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
后边的数组,竟然是个坑!这面试题也是没谁了。。。明明写着array size,打印的却是数组的地址。。。等同于%p
面试官赢了
WM_CH 发表于 2017-8-14 20:24
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
后边的数组,竟然是个坑!这面试题也是没谁 ...
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
这是因为您使用了C++。
C++是强类型的,C的类型就比较弱了,兼容的类型胡乱赋值也能也无法报错或警告。
其实LZ例子的一大堆数据类型中,只有sizeof(char)是明确规定等于1外,其他的全都是依赖于编译器的。 takashiki 发表于 2017-8-14 20:37
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
这是因为您使用了C++。
C++是强类型的,C ...
没错,这些类型确实依赖编译器。
但有些面试的题目不会给你标明具体的编译器 takashiki 发表于 2017-8-14 20:37
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
这是因为您使用了C++。
C++是强类型的,C ...
多谢指点 推荐尽可能用stdint.h里面定义的 uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t这些 Gorgon_Meducer 发表于 2017-8-16 19:11
推荐尽可能用stdint.h里面定义的 uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int6 ...
嗯,多谢大神补充。 公布一下答案啊
页:
[1]