程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C語言中基本整型數據類型

C語言中基本整型數據類型

編輯:關於C語言

C語言中的基本整型數據類型

整型家族包括字符、短整型、整型、長整型,它們都分為有符號和無符號兩種版本

整型值相互之間的大小的規則:

長整型至少應該和整型一樣長,而整型至少應該和短整型一樣長

一般情況下各種類型在32、64位機上的大小

\

long,unsigned long pointer為機器全字長,其實也就是這幾種類型的大小和CPU的位數是一樣的。<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+z8LD5rXEs8zQ8srH1NpYODa83Lm5u7e+s8nPo6zKudPDZ2NjIHZlcnNpb24gNC43LjIgMjAxMjExMDkgKFJlZCBIYXQgNC43LjItOCkgKEdDQymx4NLrtcO1vbXEPC9wPgo8cD48L3A+CjxwcmUgY2xhc3M9"brush:java;">#include int main() { printf("the char range is %-20hhd~%20hhu, size is %u\n", 0x80, 0x7f, sizeof(char)); printf("the usigned char range is %-20hhu~%20hhu, size is %u\n", 0x0, 0xff, sizeof(unsigned char)); printf("the short range is %-20hd~%20hu, size is %u\n", 0x8000, 0x7fff, sizeof(short)); printf("the unsigned short range is %-20hu~%20hu, size is %u\n", 0x0, 0xffff, sizeof(unsigned short)); printf("the int range is %-20d~%20u, size is %u\n", 0x80000000, 0x7fffffff, sizeof(int)); printf("the unsigned int range is %-20u~%20u, size is %u\n", 0x0, 0xffffffff, sizeof(unsigned int)); printf("the long range is %-20ld~%20lu, size is %u\n", 0x80000000, 0x7fffffff, sizeof(long)); printf("the unsigned long range is %-20lu~%20lu, size is %u\n", 0x0, 0xffffffff, sizeof(unsigned long)); printf("the llong range is %-20lld~%20llu, size is %u\n", 0x8000000000000000, 0x7fffffffffffffff, sizeof(long long)); printf("the unsigned llong range is %-20lld~%20llu, size is %u\n", 0x0ll, 0xffffffffffffffff, sizeof(unsigned long long)); printf("default constant size is %u %u %u\n", sizeof('b'), sizeof(0x0), sizeof(0x0ll)); }

運行結果

the char range is -128 ~ 127, size is 1
the usigned char range is 0 ~ 255, size is 1
the short range is -32768 ~ 32767, size is 2
the unsigned short range is 0 ~ 65535, size is 2
the int range is -2147483648 ~ 2147483647, size is 4
the unsigned int range is 0 ~ 4294967295, size is 4
the long range is -2147483648 ~ 2147483647, size is 4
the unsigned long range is 0 ~ 4294967295, size is 4
the llong range is -9223372036854775808~ 9223372036854775807, size is 8
the unsigned llong range is 0 ~18446744073709551615, size is 8

default constant size is 4 4 8


整型字面值(整型常量)

字面值這個術語是字面值常量的縮寫------這是一種實體,指定了自身的值,並且不允許發生改變。

當程序內出現整型常量時,它是屬於整型家族類型中的哪一種?答案取決於常量是如何書寫的,可以在有些常量的後面添加一個後綴來改變缺省規則。在整數常量後面添加L或l,可以使這個整數被解釋為long整型值,字符U或u則用於吧數值指定為unsigned 整型值,如果一個常量後面添加這兩個字符中的各一個,那麼它就被解釋為unsigned long整型值。對於字符常量。它們的類型總是int。不能再它們後面添加unsigned或者long後綴

C語言一般默認整型常量(字符常量/整型常量)類型為int,

如果使用格式字符%llu,printf會認為要輸出的這個值的大小為8字節,所以如果對應的參數如果為0x0的話則有可能打印出亂七八糟的東西(因為它讀了8個字節而不是4個字節)

另外printf整型變量時,打印的是有符號還是無符號取決於格式字符,與實際傳入的參數類型及符號無關。

例如

int i = 0x80001010;

printf("%d\n", i);打印-2147479536

printf("%u\n", i);打印2147487760

printf("%hu\n",i);打印4112高兩位被截斷,打印的實際是0x1010

printf("%hhu\n",i);打印16高三位被截斷,打印的實際是0x10

所以請謹記使用printf打印變量的大小與符號與變量本身無關,即使強轉也不行!應選擇對應正確的格式字符


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved