LoginSignup
9
2

More than 5 years have passed since last update.

Ancient C探訪記:基本型編

Posted at

おことわり: この記事では「1975年頃のC言語」仕様を解説します。2017年現在のC言語仕様とは異なるため、あなたのC言語ライフには役立たないことを予めご承知おきください。

(本投稿は Ancient C探訪記 シリーズの一部です。)

つづいて、プログラムの構成要素である「基本型(fundamental types)」を見ていきます。

Ancient Cの基本型

"C Reference Manual" §4. What’s in a Name? より引用します。4種類の基本型 int, char, float, double が定義されています。

C supports four fundamental types of objects: characters, integers, single-, and double-precision floating-point numbers.

  • Characters (declared, and hereinafter called, char) are chosen from the ASCII set; they occupy the rightmost seven bits of an 8bit byte. It is also possible to interpret chars as signed, 2's complement 8bit numbers.
  • Integers (int) are represented in 16bit 2's complement notation.
  • Single precision floating point (float) quantities have magnitude in the range approximately 10±38 or 0; their precision is 24 bits or about seven decimal digits.
  • Double-precision floating-point (double) quantities have the same range as floats and a precision of 56 bits or about 17 decimal digits.

それぞれ char は8bit文字型(符号有無は任意)、int は符号付き16bit整数型、float は単精度浮動小数点数型(32bit幅)、double は単精度浮動小数点数型(64bit幅)となっています。

標準規格との差分

Ancient Cに加えて、標準規格(ANSI C、C90)では下記の基本型が追加されます。

  • 文字型
    • signed char
    • unsigned char
  • 符号付き整数型
    • (signed) short int
    • (signed) long int
  • 符号なし整数型
    • unsigned short int
    • unsigned int
    • unsigned long int
  • 浮動小数点数型
    • long double

符号なし整数型?

Ancient Cの整数型は、符号付き整数型 int しか定義されません。符号なし整数型を扱いたい場合は、“char * などのポインタ型で代用” していたようです。は??

ポインタ型は非負値をとるメモリアドレスを保持する型ですから、メモリアドレスを整数値とみなすことで符号付き整数型(unsigned int)演算を行うという理屈です。例えば、2つの符号なし整数 a, b のうち最大値を返す関数 max は、次のよう記述されていました:

ancient-max.c
/* Return the logical maximum
 * of the 2 arguments.
 */
max(a, b)
char *a, *b;
{
  if (a > b)
    return(a);
  return(b);
}

Ancient Cの関数宣言は標準Cとは色々と差異があるのですが、ここでは関数パラメータ a, b がポインタ型(char *)として宣言されています。参考までに、標準C(ANSI C、C90)で同関数を記述すると次のようになります:

stdc-max.c
unsigned int max(unsigned int a, unsigned int b)
{
  if (a > b)
    return a;
  return b;
}

適当に明日以降につづきます。

9
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
2