搜索
bottom↓
回复: 9

数据类型基础回顾

[复制链接]

出0入0汤圆

发表于 2017-8-14 19:49:21 | 显示全部楼层 |阅读模式
大家好,

        最近在面试,被打击了,好多基础都忘记了,做了些实验,希望对大家有所帮助。

  1. #include <stdio.h>


  2. typedef enum        {
  3.         K1,
  4.         K2
  5. }exa_enum;


  6. int main(int argc, char **argv) {
  7.        

  8.         exa_enum hello = 123;

  9.         printf("*********type define specifier size test start  ************ \n\n");

  10.         printf(                                        \
  11.         "\tsizeof(int):%d bytes\n"
  12.         "\tsizeof(char):%d bytes\n"
  13.         "\tsizeof(long int):%d bytes\n"
  14.         "\tsizeof(short int):%d bytes\n"
  15.         "\tsizeof(long long int):%d bytes\n"
  16.         "\tsizeof(long double):%d bytes\n"
  17.         "\n",                                        \
  18.         sizeof(int),                                \
  19.         sizeof(char),                                 \
  20.         sizeof(long int) ,                        \
  21.         sizeof(short int) ,                        \
  22.         sizeof(long long int) ,                        \
  23.         sizeof(long double)                        \
  24.         );

  25.         printf("*********constant pointer and pointer constant test start  ************ \n\n");

  26.         char *str1 = "hello World";

  27.         // char const *ptr1 = str1; // you can also write constant pointer like this
  28.         const char *ptr1 = str1; // constant pointer

  29.         char *str2 = "hello";

  30.         char * const ptr2 = str2; // pointer constant

  31.         // you can't do the following;
  32.         // ptr2 = str1;
  33.         // *ptr1 = 'A';

  34.        
  35.         // you can do the following
  36.         ptr1 = str2;
  37.         str2 = "fuck you!";

  38.         printf("\t ptr1:%s;ptr2:%s\n\n", ptr1, ptr2);


  39.         printf("*********enum size test start  ************ \n\n");
  40.        
  41.         printf("\tsizeof(exa_enum):%d bytes\n\tsizeof(hello):%d bytes\n", sizeof(exa_enum), sizeof(hello));
  42.        

  43.         unsigned int array_t[2][3];

  44.         printf("\tarray size %8x , enum value %d\n", array_t, hello);

  45.         return 0;
  46. }
复制代码


====================================================================================

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

出0入0汤圆

 楼主| 发表于 2017-8-14 19:54:23 | 显示全部楼层
思维定势的影响,我以为array[3][3],取数组名取得就是数组的size,记得当时做题目时蛮自信的,等回来一运行...悲剧啦

出0入0汤圆

发表于 2017-8-14 20:24:41 | 显示全部楼层
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
后边的数组,竟然是个坑!这面试题也是没谁了。。。明明写着array size,打印的却是数组的地址。。。等同于%p
面试官赢了




出0入93汤圆

发表于 2017-8-14 20:37:57 | 显示全部楼层
WM_CH 发表于 2017-8-14 20:24
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
后边的数组,竟然是个坑!这面试题也是没谁 ...

我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
这是因为您使用了C++。
C++是强类型的,C的类型就比较弱了,兼容的类型胡乱赋值也能也无法报错或警告。

其实LZ例子的一大堆数据类型中,只有sizeof(char)是明确规定等于1外,其他的全都是依赖于编译器的。

出0入0汤圆

 楼主| 发表于 2017-8-14 20:45:03 | 显示全部楼层
takashiki 发表于 2017-8-14 20:37
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
这是因为您使用了C++。
C++是强类型的,C ...

没错,这些类型确实依赖编译器。

但有些面试的题目不会给你标明具体的编译器

出0入0汤圆

发表于 2017-8-14 20:57:53 | 显示全部楼层
takashiki 发表于 2017-8-14 20:37
我用VS2010测试那个枚举不能那样赋值,提示,int不能赋值给enum
这是因为您使用了C++。
C++是强类型的,C ...

多谢指点            

出0入0汤圆

 楼主| 发表于 2017-8-15 22:20:54 | 显示全部楼层

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入296汤圆

发表于 2017-8-16 19:11:13 | 显示全部楼层
推荐尽可能用stdint.h里面定义的 uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t这些

出0入0汤圆

 楼主| 发表于 2017-8-16 20:48:20 | 显示全部楼层
Gorgon_Meducer 发表于 2017-8-16 19:11
推荐尽可能用stdint.h里面定义的 uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int6 ...

嗯,多谢大神补充。

出0入0汤圆

发表于 2017-8-17 13:34:14 | 显示全部楼层
公布一下答案啊
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-18 20:49

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表