int类型比较特殊,具体的字节数同机器字长和编译器有关。如果要保证移植性,尽量用__int16 __int32 __int64吧
__int16、__int32这种数据类型在所有平台下都分配相同的字节。所以在移植上不存在问题。
所谓的不可移植是指:在一个平台上编写的代码无法拿到另一个平台上运行时,不能达到期望的运行结果。
例如:在32为平台上(所谓32位平台是指通用寄存器的数据宽度是32)编写代码,int 类型分配4个字节,而在16位平台是则分配2个字节,那么在16位上编译出来的exe,
其中是为int分配2字节,而在32位平台上运行时,会按照4个字节来解析,显然会出错误的!!

而对于非int行,目前为止,所有的类型分配的字节数都是兼容的,即不同平台对于同一个类型分配相同的字节数!!

建议:在代码中尽量避免使用int类型,根据不同的需要可以用short,long,unsigned int 等代替。

下面是各个类型一览表【转】

64位指的是cpu通用寄存器的数据宽度是64位的。

数据类型名称 字节数 别名 取值范围
int * signed,signed int 操作系统决定,即与操作系统的"字长"有关
unsigned int * unsigned 由操作系统决定,即与操作系统的"字长"有关
__int8 1 char,signed char –128 到 127
__int16 2 short,short int,signed short int –32,768 到 32,767
__int32 4 signed,signed int –2,147,483,648 到 2,147,483,647
__int64 8 –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
bool 1 false 或 true
char 1 signed char –128 到 127
unsigned char 1 0 到 255
short 2 short int,signed short int –32,768 到 32,767
unsigned short 2 unsigned short int 0 到 65,535
long 4 long int,signed long int –2,147,483,648 到 2,147,483,647
long long 8 none (but equivalent to __int64) –9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long 4 unsigned long int 0 到 4,294,967,295
enum * 由操作系统决定,即与操作系统的"字长"有关
float 4 3.4E +/- 38 (7 digits)
double 8 1.7E +/- 308 (15 digits)
long double 8 1.7E +/- 308 (15 digits)
wchar_t 2 __wchar_t 0 到 65,535
类型标识符 类型说明 长度
(字节)
范围 备注
char 字符型 1 -128 ~ 127 -27 ~ (27 -1)
unsigned char 无符字符型 1 0 ~ 255 0 ~ (28 -1)
short int 短整型 2 -32768 ~ 32767 2-15 ~ (215 - 1)
unsigned short int 无符短整型 2 0 ~ 65535 0 ~ (216 - 1)
int 整型 4 -2147483648 ~ 2147483647 -231 ~ (231 - 1)
unsigned int 无符整型 4 0 ~ 4294967295 0 ~ (232-1)
float 实型(单精度) 4 1.18*10-38 ~ 3.40*1038 7位有效位
double 实型(双精度) 8 2.23*10-308 ~ 1.79*10308 15位有效位
long double 实型(长双精度) 10 3.37*10-4932 ~ 1.18*104932 19位有效位

转自:http://www.cppblog.com/xyjzsh/archive/2010/10/20/130554.aspx

一些与平台无关的整型:

Specific integral type limits
Specifier Common Equivalent Signing Bits Bytes Minimum Value Maximum Value
int8_t signed char Signed 8 1 ?128 127
uint8_t unsigned char Unsigned 8 1 0 255
int16_t short Signed 16 2 ?32,768 32,767
uint16_t unsigned short Unsigned 16 2 0 65,535
int32_t int Signed 32 4 ?2,147,483,648 2,147,483,647
uint32_t unsigned int Unsigned 32 4 0 4,294,967,295
int64_t long long Signed 64 8 ?9,223,372,036,854,775,808 9,223,372,036,854,775,807
uint64_t unsigned long long Unsigned 64 8 0 18,446,744,073,709,551,615

上面是一些与平台无关的数据类型,由于在32位机器和64位机器中,long占据不同的字节数,所以推荐使用上面的类

c 中关于int,unsigned int , short 各种类型总结的更多相关文章

  1. 嵌入式中的 *(volatile unsigned int *)0x500 解释

    C语言中*(volatile unsigned int *)0x500的解释: 如下: (unsigned int *)0x500:将地址0x500强制转化为int型指针*(unsigned int ...

  2. 挖一挖unsigned int和补码

    文章要讨论的是两部分: 1. 原码,反码和补码. 2. short, unsigned short, int, unsigned int, long, unsigned long的表示及转换 1. 原 ...

  3. 无符号整型 unsigned int、unsigned long、usigned long long、size_t 比较和格式控制

    位数比较 由于数据的长度和平台相关,所以基于 64 位系统比较. Windows Linux unsigned int 32 bits/4294967295 32 bits unsigned long ...

  4. c++ 中关于int,unsigned int , short的关系与应用

    转载:http://www.cppblog.com/xyjzsh/archive/2010/10/20/130554.aspx?opt=admin   int类型比较特殊,具体的字节数同机器字长和编译 ...

  5. 数32位 unsigned int中1的个数

    参考文章:http://www.cnblogs.com/graphics/archive/2010/06/21/1752421.html 最简单的方法: int BitCount0(unsigned ...

  6. [转载] java中byte数组与int,long,short间的转换

    文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...

  7. (转+原)VC编译错误:uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义

    参考网址:http://zhanyonhu.blog.163.com/blog/static/16186044201023094754832/ 1>uafxcw.lib(afxmem.obj) ...

  8. Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)

    http://darksleep.com/player/JavaAndUnsignedTypes.html —————————————————————————————————————————————— ...

  9. java中byte数组与int,long,short间的转换

    http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...

随机推荐

  1. iOS 项目中的NSNotification简单使用

    iOS中NSNotification的简单使用 好久没有写过博客了,总是遇到问题查一下,今天查的又是一个老问题,想了想,还是记录一下!今天在项目开发中遇到一个配置及时性处理的问题,想了想之后决定用通知 ...

  2. list, tuple, dict, set的用法总结

    d = [1, 2, 3, 4, 5, 5] #list print(d) e = ([1, 2], 3, 4, 5555, 5555)#tuple print(e) f = {'Michael':8 ...

  3. CentOS7 安装JDK

    链接地址:http://jingyan.baidu.com/article/c74d60007b85510f6a595dfa.html VMware 10 centos 安装jdk 及mysql 方法 ...

  4. Laravel 5.1 ACL权限控制 一

    请自行添加命名空间,代码下载地址 https://github.com/caoxt/learngit 1.所需要用到的数据表 users(用户表).roles(角色表).role_user(用户角色对 ...

  5. Nginx 之三:nginx服务器模块、web请求处理机制及事件驱动模型、进程功能和进程间通信

    一:Nginx的模块化结构设计: 1.核心模块:指的是nginx服务器运行当中必不可少的模块,这些模块提供了最基本最核心的服务,比如权限控制.进程管理.错误日志.事件驱动.正则表达式解析等,nginx ...

  6. C语言之固定格式输出当前时间

    固定格式输出当前时间 编程实现将当前时间用以下形式输出:星期  月  日   小时:分:秒   年 代码如下: #include<stdio.h>#include<stdlib.h& ...

  7. 设计模式总结5--命令模式 commend pattern

    命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象.就像我们去餐厅,点菜是找服务员,然后服务员去让厨师做菜而不是我们直接找厨师做菜 public interface Commend { ...

  8. 初遇Git与MarkDown 文件

    新年好! 昨晚熬夜在学Git,稍微会了一些命令. 推荐大家去try.github.io上学习,这是GitHub提供的网页,它在网页提供了一个“伪”模拟器,根据网页的提示学习命令.网页上说15分钟就能学 ...

  9. poj 1068 Parencodings(栈)

    题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...

  10. codility上的练习 (1)

    codility上面添加了教程.目前只有lesson 1,讲复杂度的……里面有几个题, 目前感觉题库的题简单. tasks: Frog-Jmp: 一只青蛙,要从X跳到Y或者大于等于Y的地方,每次跳的距 ...