C语言-数据类型所占字节数

Type bytes

今天特意测试了所使用电脑的各种类型所占的字节数。
在此做个记录,以便查询。

首先看一下系统信息

$ screenfetch
                          ./+o+-       patrick@Marina
                  yyyyy- -yyyyyy+      OS: Ubuntu 16.04 xenial
               ://+//////-yyyyyyo      Kernel: x86_64 Linux 4.4.0-66-generic
           .++ .:/++++++/-.+sss/`      Uptime: 12h 24m
         .:++o:  /++++++++/:--:/-      Packages: 2246
        o:+o+:++.`..```.-/oo+++++/     Shell: zsh 5.1.1
       .:+o:+o/.          `+sssoo+/    Resolution: 1920x1080
  .++/+:+oo+o:`             /sssooo.   DE: Unity 7.4.0
 /+++//+:`oo+o               /::--:.   WM: Compiz
 \+/+o+++`o++o               ++////.   WM Theme: Ambiance
  .++.o+++oo+:`             /dddhhh.   GTK Theme: Ambiance [GTK2/3]
       .+.o+oo:.          `oddhhhh+    Icon Theme: Numix-Circle
        \+.++o+o``-````.:ohdhhhhh+     Font: Ubuntu 11
         `:o+++ `ohhhhhhhhyo++os:      CPU: Intel Core i5 CPU 750 @ 2.668GHz
           .o:`.syhhhhhhh/.oo++o`      GPU: GeForce GTX 550 Ti
               /osyyyyyyo++ooo+++/     RAM: 3252MiB / 3942MiB
                   ````` +oo+++o\:    
                          `oo++.      

然后写了一个C程序,来打印所有类型的bytes值

#include 
#include 

int max[5];
int main()
{
    int size = 5;
    int *ptr = malloc(size * sizeof(int));
    printf("%ld\n", sizeof(ptr));
    printf("%ld\n", sizeof(max));

    printf("===== type bytes =====\n");
    printf(" char type: %ld\n", sizeof(char));
    printf(" short type: %ld\n", sizeof(short));
    printf(" int type: %ld\n", sizeof(int));
    printf(" long type: %ld\n", sizeof(long));
    printf(" long long type: %ld\n", sizeof(long long));
    printf(" float type: %ld\n", sizeof(float));
    printf(" double type: %ld\n", sizeof(double));
    printf(" long double type: %ld\n", sizeof(long double));
    free(ptr);
}

程序输出如下:

$ ./test 
8
20
===== type bytes =====
 char type: 1
 short type: 2
 int type: 4
 long type: 8
 long long type: 8
 float type: 4
 double type: 8
 long double type: 16

你可能感兴趣的:(Linux,C)