[置顶] C语言中各种数据类型的长度 sizeof char, short, int, long, long long
这些数据类型的sizeof具体长度依赖于编译器和操作系统(32-bit or 64-bit)
1: 首先,参见c99标准
标准中没有定义这些数据类型的长度,而是定义了这些数据类型能表达的大小范围的最小极限。
C99链接: http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf
The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold. You can infer minimum size in bits from the required range and the value of CHAR_BIT macro, that defines the number of bits in a byte (in all but the most obscure platforms it's 8). One additional constraint for char is that its size is always 1 byte, or CHAR_BIT bits (hence the name). Minimum ranges required by the standard (page 22) are: signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement platforms)
unsigned char: 0 to 255
"plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
signed short: -32767 to 32767
unsigned short: 0 to 65535
signed int: -32767 to 32767
unsigned int: 0 to 65535
signed long: -2147483647 to 2147483647
unsigned long: 0 to 4294967295
signed long long: -9223372036854775807 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615
A C++ (or C) implementation can define the size of a type in bytes sizeof(type) to any value, as long as the expression sizeof(type) * CHAR_BIT evaluates to the number of bits enough to contain required ranges, and
the ordering of type is still valid (e.g. sizeof(int) <= sizeof(long)).
The actual implementation-specific ranges can be found in <limits.h> header in C, or <climits> in C++ (or even better, templated std::numeric_limits in <limits> header).
2: 数据类型长度需要符合2个标准
一个是数据类型能描述的范围,一个是数据类型表达范围之间的顺序
C90 standard requires that
sizeof(short)<=sizeof(int)<=sizeof(long)
C99 standard requires that
sizeof(short)<=sizeof(int)<=sizeof(long)<sizeof(longlong)
3: 5种标准数据类型和他们的衍生类型
signed char
short int
int
long int
long long int
There are five standard signed integer types : signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list. For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: unsigned char, unsigned short int, unsigned int, unsigned long int, and unsigned long long int, each of which occupies the same amount of storage and has the same alignment requirements.
The C++ Standard says it like this : 3.9.1, §2 : There are five signed integer types : "signed char", "short int", "int", "long int", and "long long int". In this list, each type provides at least as much storage as those preceding it in the list. Plain ints have the natural size suggested by the architecture of the execution environment (44); the other signed integer types are provided to meet special needs. (44) that is, large enough to contain any value in the range of INT_MIN and INT_MAX, as defined in the header <climits>. The conclusion : it depends on which architecture you're working on. Any other assumption is false.
4: 实践中的事实标准
32-bit 操作系统中,事实标准为 ILP32, int, long, pointer 都是4字节
64-bit 操作系统中,事实标准为LP64, int - 4字节, long, pointer 是8字节
在linux操作系统中,参见头文件 int-ll64.h
For 32-bit systems, the 'de facto' standard is ILP32 - that is, int, long and pointer are all 32-bit quantities. For 64-bit systems, the primary Unix 'de facto' standard is LP64 - long and pointer are 64-bit (but int is 32-bit). The Windows 64-bit standard is LLP64 - long long and pointer are 64-bit (but long and int are both 32-bit). At one time, some Unix systems used an ILP64 organization. None of these de facto standards is legislated by the C standard (ISO/IEC 9899:1999), but all are permitted by it.
5: 数据模型 LP64和ILP32
数据来源: http://en.wikipedia.org/wiki/64-bit#64-bit_data_models
Data model | short (integer) | int | long (integer) | long long | pointers/ size_t |
Sample operating systems |
---|---|---|---|---|---|---|
LLP64/ IL32P64 |
16 | 32 | 32 | 64 | 64 | Microsoft Windows (X64/IA-64) |
LP64/ I32LP64 |
16 | 32 | 64 | 64 | 64 | Most Unix and Unix-like systems, e.g. Solaris, Linux, BSD, and OS X; z/OS |
数据来源:http://docs.oracle.com/cd/E19620-01/805-3024/lp64-1/index.html
Table F-1 C Data Type Sizes
C Type |
ILP32 |
LP64 |
---|---|---|
char |
8 |
8 |
short |
16 |
16 |
int |
32 |
32 |
long |
32 |
64 |
long long |
64 |
64 |
pointer |
32 |
64 |
In addition to the data model changes, some system-derived types, such as size_t, have been expanded to be 64-bit quantities when
compiled in the 64-bit environment.
同上
6: linux 中的实际使用
#ifndef __ASSEMBLY__
/*
* __xx is ok: it doesn't pollute the POSIX namespace. Use these in the
* header files exported to user space
*/ typedef __signed__ char __s8;
typedef unsigned char __u8; typedef __signed__ short __s16;
typedef unsigned short __u16; typedef __signed__ int __s32;
typedef unsigned int __u32; #ifdef __GNUC__
__extension__ typedef __signed__ long long __s64;
__extension__ typedef unsigned long long __u64;
#else
typedef __signed__ long long __s64;
typedef unsigned long long __u64;
#endif
也就是没用到long 类型,用了char, short, int, long long 就够了。
7. printf
http://www.gnu.org/software/libc/manual/html_mono/libc.html#Integer-Conversions 12.12.4 Integer Conversions This section describes the options for the ‘%d’, ‘%i’, ‘%o’, ‘%u’, ‘%x’, and ‘%X’ conversion specifications. These conversions print integers in various formats. The ‘%d’ and ‘%i’ conversion specifications both print an int argument as a signed decimal number; while ‘%o’, ‘%u’, and ‘%x’ print the argument as an unsigned octal, decimal, or hexadecimal number (respectively). The ‘%X’ conversion specification is just like ‘%x’ except that it uses the characters ‘ABCDEF’ as digits instead of ‘abcdef’. ‘l’
Specifies that the argument is a long int or unsigned long int, as appropriate. Two ‘l’ characters is like the ‘L’ modifier, below.
If used with ‘%c’ or ‘%s’ the corresponding parameter is considered as a wide character or wide character string respectively. This use of ‘l’ was introduced in Amendment 1 to ISO C90. ‘L’
‘ll’
‘q’
Specifies that the argument is a long long int. (This type is an extension supported by the GNU C compiler. On systems that don't support extra-long integers, this is the same as long int.)
The ‘q’ modifier is another name for the same thing, which comes from 4.4 BSD; a long long int is sometimes called a “quad” int.
8: 字节对齐
http://www.unix.org/whitepapers/64bit.html
http://software.intel.com/en-us/articles/data-alignment-when-migrating-to-64-bit-intel-architecture
https://en.wikipedia.org/wiki/Data_structure_alignment
http://csweapon.diandian.com/post/2011-08-26/4372667
自然对齐
64-bit operating environment
- Align 8-bit data at any address
- Align 16-bit data to be contained within an aligned four-byte word
- Align 32-bit data so that its base address is a multiple of four
- Align 64-bit data so that its base address is a multiple of eight
- Align 80-bit data so that its base address is a multiple of sixteen
- Align 128-bit data so that its base address is a multiple of sixteen
|
Source: |
#include <stdio.h> int main(void) { |
ILP32 member lengths: |
length li = 8 |
LP64 member lengths: |
length li = 16 |
插曲:
我为什么写这篇文章。前不久去某公司面试,boss问我int在64-bit OS上是多少,我说是4字节。然后他说是8字节,我表示又学到了很多知识。我其实以前就看过关于这个数据模型的一些帖子,只是没这么仔细。于是今天整理了一下。
[置顶] C语言中各种数据类型的长度 sizeof char, short, int, long, long long的更多相关文章
- [置顶]
C语言中 || 和 &&
|| 或操作,|| 为界将表达式分为两部分,他会先算前一部分,如果前一部分为真,他将停止运算,如果为假,他才会算第二部分,你这里第一部分就为真了,第二部分当然也就不会算了. 例如: a || b , ...
- c语言中各种数据类型的长度
在32位平台和64位平台上,同一种数据类型可能有不同的数据长度: 类型 32位平台 64位平台 char 1 1 short 2 2 int 4 4 long 4 8 long long 8 8 fl ...
- 2_C语言中的数据类型 (五)char
1.1 char类型 1.1.1 char常量,变量 char c:定义一个char变量 ‘a’,char的常量 Char的本质就是一个整数,一个只有1个字节大小的整数 ...
- Android For JNI(二)——C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器
Android For JNI(二)--C语言中的数据类型,输出,输入函数以及操作内存地址,内存修改器 当我们把Hello World写完之后,我们就可以迈入C的大门了,今天就来讲讲基本的一些数据类型 ...
- C语言中求字符串的长度
在C语言中求字符串的长度,可以使用sizeof()函数和strlen()函数,后者需要引入string.h (#include <string.h>) 因为C语言字符串是以 \0 结尾表示 ...
- 在C语言中基本数据类型所占的字节数
基本数据类型所占的字节数其实跟C语言本身没有太大的关系,它取决于编译器的位数,下面这张表说明了不同编译器下基本数据类型的长度: 32位编译器中各基本类型所占字节数: 注:对于32位的编译器,指针变量的 ...
- 2_C语言中的数据类型 (七)printf与scanf
1 字符串格式化输出和输入 1.1 字符串在计算机内部的存储方式 字符串是内存中一段连续的char空间,以’\0’结尾 “”是C语言表达字符串的方式 1.2 ...
- 2_C语言中的数据类型 (四)整数与无符号数
1.1 sizeof关键字 sizeof是c语言关键字,功能是求指定数据类型在内存中的大小,单位:字节 sizeof与size_t类型 1.1 int类型 1.1.1 ...
- [置顶]
C语言学习入门
编译文件:cc -c one.c two.c 生成.o目标文件 链接文件:cc one.o two.o 默认生成 a.out 执行文件 指定生成的可执行文件名 cc -o one one ...
随机推荐
- asp.net mvc3 数据验证(三)—自定义数据注解
原文:asp.net mvc3 数据验证(三)-自定义数据注解 前两节讲的都是asp.net mvc3预先设定的数据注解,但是系统自由的数据注解肯定不适合所有的场合,所以有时候我们需要 ...
- Cocos2d-x3.0之路--02(引擎文件夹分析和一些细节)
关于怎么搭建好开发环境的我就不写了,网上非常多. 那么 我们来看看 引擎文件的文件夹 所谓知己知彼 百战不殆嘛 先说一下setup.py 这个文件是有关配置的python文件,比方我们在进行andro ...
- ExtJS4 便捷三层开发模式
ExtJS4 便捷三层开发模式 定义类已经不是ext4.x一个新特性,但与ext3.x的自定义类有语法上的区别.将相关模块封装成类可以有效的减少浏览器的压力,提高渲染速度,同时抽象每一个可重用方法,减 ...
- JDK-windows7环境变量配置-亲测版本 以及HelloWorld
1.下载并安装jdk,假设安装1.6.0_45版本到C:\Program Files\Java,则安装完毕后,目录结构为: C:\PROGRAM FILES\JAVA├─jdk1.6.0_45│ ├─ ...
- unity3d 血液
这里的人物头像和血条是在3d世界生成的,所以有真正的纵深感和遮挡关系,废话不多说,看我是怎么实现的. 第一步.先在UI Root里制作头像和血条. 这个制作步骤基本和我前面一篇文章介绍头像血条的制作步 ...
- ASP.NET MVC企业级项目框架
ASP.NET MVC企业级项目框架 MVC项目搭建笔记---- 项目框架采用ASP.NET MVC+Entity Framwork+Spring.Net等技术搭建,搭建过程内容比较多,结合了抽象工厂 ...
- Ora创建job定时执行某存储过程
--创建job任务,每天晚上8点执行存储过程:por_postrecords-- declare job number; begin sys.dbms_job.submit(job =>job, ...
- SQL SERVER – Import CSV File Into SQL Server Using Bulk Insert – Load Comma Delimited File Into SQL Server
CSV stands for Comma Separated Values, sometimes also called Comma Delimited Values. Create TestTabl ...
- 【Android中Broadcast Receiver组件具体解释
】
BroadcastReceiver(广播接收器)是Android中的四大组件之中的一个. 以下是Android Doc中关于BroadcastReceiver的概述: ①广播接收器是一个专注于接收广播 ...
- Java凝视Override、Deprecated、SuppressWarnings详细解释
一.什么是视线 说起目光,你必须先提什么是元数据(metadata). 所谓元数据是数据的数据.那.元数据是描述数据的叙述. 像在表中的数据字段,叙述了这个字段下的数据的含义.而J2SE5.0 ...