4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ancient C探訪記:演算子編

Last updated at Posted at 2017-12-20

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

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

お次は「演算子(Operators)」です。どんどん行きましょう。雑な進行

Ancient Cの演算子

複合代入 =op

"C Reference Manual" §7.14 Assignment operators 以下の §7.14.2 〜 7.14.11 から引用します。Ancient Cの複合代入(compound assignment)は イコール(=)の後ろに演算子を配置 する構文です。

7.14.2 lvalue =+ expression
7.14.3 lvalue =- expression
7.14.4 lvalue =* expression
7.14.5 lvalue =/ expression
7.14.6 lvalue =% expression
7.14.7 lvalue =>> expression
7.14.8 lvalue =<< expression
7.14.9 lvalue =& expression
7.14.10 lvalue =^ expression
7.14.11 lvalue = | expression
The behavior of an expression of the form E1 = E1 op E2; however, E1 is evaluated only once. Moreover, expressions like i =+ p in which a pointer is added to an integer, are forbidden.

ancient-assign.c
int x = 42;

x =/ 2;  /* x = x / 2; と等価 */
x =- 4;  /* x = x - 4; と等価 */

実はこの構文は致命的な問題を抱えています。x=-1という表記が、複合代入演算(=-)と、符号(-)明示した値(1)の代入(=)とで曖昧になってしまいます。空白文字を入れてx= -1と書かない限り、最長マッチにより=-と解釈されたようです。

ご存知の通り、標準規格(ANSI C、C90)では "イコール(=)の前に演算子を配置” する構文に変更されています。この構文ではAncient Cのような曖昧さは生じません。1

単項演算子 +

Ancient Cには 単項演算子+ が存在しません2。単項演算子(Unary operators)としては、*, &, -, !, ~の5種類のみが定義されます。

sizeof 演算子

"C Reference Manual" §7.2.10 sizeof expression より引用します。Ancient Cにおけるsizeof演算子は、式(expression)のみが対象となります。

The sizeof operator yields the size, in bytes, of its operand. When applied to an array, the result is the total number of bytes in the array. The size is determined from the declarations of the objects in the expression. This expression is semantically an integer constant and may be used anywhere a constant is required. Its major use is in communication with routines like storage allocators and I/O systems.

標準Cにおける sizeof ( 型名 ) のように、型名(type-name)からサイズを直接取得できません。

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

  1. これは後にDennis Ritchie氏自身が誤り(mistake)であったと認めています。"The Development of the C Language"より引用します; "In B and early C, the operator was spelled =+ instead of +=; this mistake, repaired in 1976, was induced by a seductively easy way of handling the first form in B's lexical analyzer."

  2. “単項演算子+”は +17 のように正符号を明示するものです。単項演算子とは別に、算術加算の2項演算子+(例:1+2)はAncient Cにも存在しています。

4
0
1

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
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?