C的自定义类型比较少, 占用字节数可以使用sizeof 操作符来获取.
char, short, int, long, float, double
char 实际存储为数字.占用1字节.
可使用的前缀 : unsigned, long
下面在一台64位的LINUX上得到的结果 :
[root@db-172-16-3-150 zzz]# cat n.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
int i;
int main() {
fprintf(stdout, "sizeof(%s)=%lu\n", "short", sizeof(short));
fprintf(stdout, "sizeof(%s)=%lu\n", "int", sizeof(int));
fprintf(stdout, "sizeof(%s)=%lu\n", "char", sizeof(char));
fprintf(stdout, "sizeof(%s)=%lu\n", "long", sizeof(long));
fprintf(stdout, "sizeof(%s)=%lu\n", "float", sizeof(float));
fprintf(stdout, "sizeof(%s)=%lu\n", "double", sizeof(double));
fprintf(stdout, "sizeof(%s)=%lu\n", "long int", sizeof(long int));
fprintf(stdout, "sizeof(%s)=%lu\n", "long long int", sizeof(long long int));
fprintf(stdout, "sizeof(%s)=%lu\n", "long long", sizeof(long long));
fprintf(stdout, "sizeof(%s)=%lu\n", "long double", sizeof(long double));
return 0;
}
结果 :
[root@db-172-16-3-150 zzz]# gcc -O3 -Wall -Wextra -Werror -g ./n.c -o n && ./n
sizeof(short)=2
sizeof(int)=4
sizeof(char)=1
sizeof(long)=8
sizeof(float)=4
sizeof(double)=8
sizeof(long int)=8
sizeof(long long int)=8
sizeof(long long)=8
sizeof(long double)=16