-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #902 from macrat/article/what-is-uint16
古い記事の誤りを修正
- Loading branch information
Showing
1 changed file
with
24 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
--- | ||
title: C言語のu_int16とかって何なのよ。 | ||
pubtime: 2013-04-28T02:25:00+09:00 | ||
modtime: 2024-04-28T00:04:00+09:00 | ||
tags: [C言語] | ||
description: C言語のint8とかu_int16とかの意味とサイズ、最大値と最小値のまとめです。 | ||
description: C言語のint8やu_int16といった型の意味とサイズ、最大値と最小値のまとめです。 | ||
--- | ||
|
||
unixのヘッダとかで見る`u_int8`とか、`u_int16`、`u_int32`ってやつ。アレは何なのか。というお話。 | ||
Unix向けに書かれたC言語のプログラムで見る`u_int8`や、`u_int16`、`u_int32`って型は何なのか。というお話。 | ||
そんな検索ワードで来てくださった方が居たようなので。 | ||
|
||
端的に言ってしまえば、`u_int8`は8ビットの`unsigned int`を指します。 | ||
つまり`u_int8` = `unsigned char`ってこと。 | ||
たとえば`u_int16`の場合、最初の`u_`は**unsigned**であること = 0未満を扱わないことを、最後の`16`は**16bit**分のサイズがあることを示しています。 | ||
`u_`を外して**signed**になってマイナスを扱えるようになりますし、`16`の代わりに`8`や`32`にすると格納できる容量が変わります。 | ||
|
||
|名前 |別名 |サイズ |最小値 |最大値 | | ||
|----------------------|------------------|---------------------|-------------------------:|-------------------------:| | ||
|short int |int8_t |16bit | -32,768| 32,767| | ||
|unsigned short int |u_int8 / uint8_t |16bit | 0| 65,535| | ||
|int | |32bit(とも限らない)| -2,147,483,648| 2,147,483,647| | ||
|unsigned int |uint |32bit(とも限らない)| 0| 4,294,967,295| | ||
|long int |int32_t |32bit | -2,147,483,648| 2,147,483,647| | ||
|unsigned long int |u_int32 / uint32_t|32bit | 0| 4,294,967,295| | ||
|long long int |int64_t |64bit |-9,223,372,036,854,775,808| 9,223,372,036,854,775,807| | ||
|unsigned long long int|u_int64 / uint64_t|64bit | 0|18,446,744,073,709,551,615| | ||
似たような型を並べてみると以下のようになります。 | ||
|
||
だいたいこんな感じかな。 | ||
| 名前 | 別名 | サイズ | 最小値 | 最大値 | | ||
|------------------------|-------------------------------------|----------:|---------------------------:|---------------------------:| | ||
| unsigned char | char / u_int8 / uint8_t | 8bit | 0 | 255 | | ||
| signed char | int8 / int8_t | 8bit | -128 | 127 | | ||
| short int | short / int16_t | 16bit | -32,768 | 32,767 | | ||
| unsigned short int | unsigned short / u_int16 / uint16_t | 16bit | 0 | 65,535 | | ||
| int | signed int | 32bit (※) | -2,147,483,648 | 2,147,483,647 | | ||
| unsigned int | uint | 32bit (※) | 0 | 4,294,967,295 | | ||
| long int | int32_t | 32bit | -2,147,483,648 | 2,147,483,647 | | ||
| unsigned long int | u_int32 / uint32_t | 32bit | 0 | 4,294,967,295 | | ||
| long long int | int64_t | 64bit | -9,223,372,036,854,775,808 | 9,223,372,036,854,775,807 | | ||
| unsigned long long int | u_int64 / uint64_t | 64bit | 0 | 18,446,744,073,709,551,615 | | ||
|
||
他にもVisual Studioだと`__int8`、`__int16`、`__int32`、`__int64`などなど。 | ||
ちなみに悪名高き(?)`DWORD`型は符号なしの32ビット。Dobleじゃないただの`WORD`型は符号なしの16ビットだよ。 | ||
勝手に型を増やすなよめんどくさい。 | ||
※ int / unsigned int はOSによってサイズが異なる場合があります。 | ||
|
||
ちなみに、Windowsだと`__int8`、`__int16`、`__int32`、`__int64`もあります。 | ||
さらには`DWORD`(符号なし32ビット)、`WORD`(符号なし16ビット)なども。 | ||
同じ型でもいろいろ呼び方があってややこしいですね。 | ||
|
||
--- | ||
|
||
参考: | ||
- [基本的な決まり - 目指せプログラマー!](http://www5c.biglobe.ne.jp/~ecb/c/01_04.html) | ||
- [Solaris 動的トレースガイド](https://docs.oracle.com/cd/E19253-01/819-0395/chp-typeopexpr-2/index.html) | ||
- [データ型の範囲 - Visual Studio](http://msdn.microsoft.com/ja-jp/library/vstudio/s3f49ktz.aspx) |