天天看點

【C/C++】C/C++基本資料類型

标準C基本資料類型:int char long short float double void以及它們與signed、unsigned的組合。标準C++增加了bool型和wchar_t型,在32位作業系統上,它們的長度如下表:

類型辨別符

類型說明

長度(位元組)

範圍

備注

char

字元型

1

-128 ~ 127

-27 ~ (27 -1)

unsigned char

無符字元型

0 ~ 255

0 ~ (28 -1)

short int

短整型

2

-32768 ~ 32767

2-15 ~ (215 - 1)

unsigned short int

無符短整型

0 ~ 65535

0 ~ (216 - 1)

long int

長整型

4

-2147483648 ~ 2147483646

-231 ~ (231 - 0)

int

整型

-2147483648 ~ 2147483647

-231 ~ (231 - 1)

unsigned int

無符整型

0 ~ 4294967295

0 ~ (232-1)

float

實型(單精度)

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位有效位

在不同的平台下,字長不同,具體可以通過以下代碼檢視:

#include<stdio.h>  

void main()  

{  

    //字元型 有符号和無符号的位元組數一樣 隻是範圍不同  

    printf("char is %d\n",sizeof(char));  

    printf("unsigned char is %d\n",sizeof(unsigned char));  

    //整數型  

    printf("short int is %d\n",sizeof(short int));  

    printf("unsigned short int is %d\n",sizeof(unsigned short int));  

    printf("long int is %d\n",sizeof(long int));  

    printf("int is %d\n",sizeof(int));  

    printf("unsigned int is %d\n",sizeof(unsigned int));  

    //浮點型  

    printf("float is %d\n",sizeof(float));  

    printf("double is %d\n",sizeof(double));  

    printf("long double is %d\n",sizeof(long double));  

}  

在頭檔案climits.h中,定義了符号常量來表示類型的限制。其清單如下:

name

expresses

value*

CHAR_BIT

Number of bits in a <code>char</code> object (byte)

<code>8</code> or greater

SCHAR_MIN

Minimum value for an object of type <code>signed char</code>

<code>-127</code> (<code>-27+1</code>) or less

SCHAR_MAX

Maximum value for an object of type <code>signed char</code>

<code>127</code> (<code>27-1</code>) or greater

UCHAR_MAX

Maximum value for an object of type <code>unsigned char</code>

<code>255</code> (<code>28-1</code>) or greater

CHAR_MIN

Minimum value for an object of type <code>char</code>

either SCHAR_MIN or <code>0</code>

CHAR_MAX

Maximum value for an object of type <code>char</code>

either SCHAR_MAX or UCHAR_MAX

MB_LEN_MAX

Maximum number of bytes in a multibyte character, for any locale

<code>1</code> or greater

SHRT_MIN

Minimum value for an object of type <code>short int</code>

<code>-32767</code> (<code>-215+1</code>) or less

SHRT_MAX

Maximum value for an object of type <code>short int</code>

<code>32767</code> (<code>215-1</code>) or greater

USHRT_MAX

Maximum value for an object of type <code>unsigned short int</code>

<code>65535</code> (<code>216-1</code>) or greater

INT_MIN

Minimum value for an object of type <code>int</code>

INT_MAX

Maximum value for an object of type <code>int</code>

UINT_MAX

Maximum value for an object of type <code>unsigned int</code>

LONG_MIN

Minimum value for an object of type <code>long int</code>

<code>-2147483647</code> (<code>-231+1</code>) or less

LONG_MAX

Maximum value for an object of type <code>long int</code>

<code>2147483647</code> (<code>231-1</code>) or greater

ULONG_MAX

Maximum value for an object of type <code>unsigned long int</code>

<code>4294967295</code> (<code>232-1</code>) or greater

LLONG_MIN

Minimum value for an object of type <code>long long int</code>

<code>-9223372036854775807</code> (<code>-263+1</code>) or less

LLONG_MAX

Maximum value for an object of type <code>long long int</code>

<code>9223372036854775807</code> (<code>263-1</code>) or greater

ULLONG_MAX

Maximum value for an object of type <code>unsigned long long int</code>

<code>18446744073709551615</code> (<code>264-1</code>) or greater

* 其精确值依賴于你的系統和庫的實作

繼續閱讀