jump to navigation

c-faq review and a chinese translation (1.3) January 24, 2007

Posted by TSAI HONG-BIN in Programming.

1.3 Since C doesn’t define sizes exactly, I’ve been using typedefs like int16 and int32. I can then define these typedefs to be int, short, long, etc. depending on what machine I’m using. That should solve everything, right?

If you truly need control over exact type sizes, this is the right approach. There remain several things to be aware of:

  • There might not be an exact match on some machines. (There are, for example, 36-bit machines.)
  • A typedef like int16 or int32 accomplishes nothing if its intended meaning is “at least” the specified size, because types int and long are already essentially defined as being “at least 16 bits” and “at least 32 bits,” respectively.
  • Typedefs will never do anything about byte order problems (e.g. if you’re trying to interchange data or conform to externally-imposed storage layouts).
  • You no longer have to define your own typedefs, because the Standard header <inttypes.h> contains a complete set.

See also questions 10.16 and 20.5.

既然 C 語言不明確的定義資料型態的大小,我已經用 typedefs 例如 int16 int32。於是我可以依據我所使用的機器不同,來定義這些 typedefs int, short long …etc. 這樣應該可以解決問題,不是嗎?


Ÿ 某些機器上或許沒有精確符合的大小,如:36-bit machine

Ÿ 如果目的是去定義該型態的最小 bit 數。那麼 typedefs int16 int32 並不能幫上什麼忙,因為資料型態 int long 基本上已經被定義為「至少16 bits」和「至少 32 bits

Ÿ Typedefs並不能處理 byte order 的問題 (e.g. 如果你試圖要和外掛的儲存結構交換資料,或使與其一致) (按:似乎指的是 big-endian little-endian)

Ÿ 你不再需要去定義你自己的typedefs,因為標準的表頭檔 (inttypes.h) 已經包含所有的集合。



1. spencer_hill - February 23, 2007

beautiful online information center. greatest work thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: