=+ 是什么意思? (等于加)在C中是什么意思?

发布于 2024-12-06 13:09:09 字数 87 浏览 0 评论 0原文

我今天在一些 C 代码中遇到了 =+,而不是标准的 +=;我不太确定这里发生了什么事。我在文档中也找不到它。

I came across =+ as opposed to the standard += today in some C code; I'm not quite sure what's going on here. I also couldn't find it in the documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

送你一个梦 2024-12-13 13:09:09

在 C 的古代版本中,=+ 相当于 +=。它的残余物与最早的恐龙骨头一起被发现。

例如,B 引入了广义赋值运算符,使用 x+=yy 添加到 x。该符号来自 McIlroy 的 Algol 68,他将其合并到他的 TMG 版本中。 (在 B 和早期 C 中,运算符被拼写为 =+ 而不是 +=;这个错误于 1976 年修复,是由处理第一个错误的一种诱人的简单方法引起的。 B 的词法分析器中的形式。)

[C 语言的发展,Dennis Ritchie。版权所有 ACM,1993。省略内部引用。]

自 20 世纪 70 年代中期以来,它没有特殊含义 —— 它只是一个 = 后面跟着一个 +

In ancient versions of C, =+ was equivalent to +=. Remnants of it have been found alongside the earliest dinosaur bones.

For example, B introduced generalized assignment operators, using x+=y to add y to x. The notation came from Algol 68 via McIlroy, who incorporated it in his version of TMG. (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.)

[The Development of the C Language, Dennis Ritchie. Copyright ACM, 1993. Internal citations omitted.]

Since the mid-1970's, it has no special meaning -- it's just a = followed by a +.

我要还你自由 2024-12-13 13:09:09

您可以在 1979 年 1 月的第 7 版 UNIX 手册(第 2a 卷)中找到旧表示法的证据,该手册可在线访问 http://cm.bell-labs.com/7thEdMan/(大约自 2015 年 7 月起不再提供;2015 年 6 月的版本现已通过 WayBack Machine 提供: http://cm.bell-labs。 com/7thEdMan/ — 或位于 https://9p.io/7thEdMan/)。

该章的标题为 Dennis M. Ritchie 的“C 参考手册”,并且包含在该手册的 PDF 版本中,但没有 HTML 版本。
在相关部分,它说:

7.14.1 左值=表达式

表达式的值替换左值引用的对象的值。操作数不需要有
类型相同,但两者都必须是 int、char、float、double 或指针。如果两个操作数都不是指针,则赋值
按预期发生,可能先于右侧表达式的转换。
当两个操作数都是 int 或任何类型的指针时,不会发生任何转换;表达式的值
只是存储到左值引用的对象中。因此可以生成导致寻址的指针
使用时出现异常。

7.14.2 左值 =+ 表达式
7.14.3 左值=-表达式
7.14.4 左值 =* 表达式
7.14.5 左值=/表达式
7.14.6 左值=%表达式
7.14.7 左值=>>表达式
7.14.8 左值=<<表达式
7.14.9 左值=&表达式
7.14.10 左值 =^ 表达式
7.14.11 左值 = |表达方式

“E1 =op E2”形式的表达式的行为可以通过将其视为等价来推断
''E1 = E1 操作 E2'';但是,E1 仅评估一次。此外,像“i =+ p”这样的表达式,其中指针是
禁止添加到整数。


另外,L Rosler 在“UNIX® SYSTEM:阅读和应用,卷 II”中发表了一篇论文“C 的进化”,最初由 AT&T 作为其 1984 年 10 月的技术期刊出版,后来由 Prentice-Hall 于 1987 年出版(ISBN 0-13-939845-7)。其中一部分是:

III。管理不兼容的变更

不可避免地,所做的一些更改会改变现有有效程序的语义。那些维护内部使用的各种编译器的人试图确保程序员得到足够的警告,这样的更改将生效,并且新编译器版本的引入不会强制立即重新编译所有程序。

例如,在最早的实现中,不明确的表达式x=-1被解释为“将x减1”。现在它被解释为“将值 -1 赋给 x”。这一变化发生在三个年度主要版本的过程中。首先,编译器和 lint 程序验证器已更改,以生成有关存在“老式”赋值操作(例如 =-)的消息警告。接下来,解析器被更改为新的语义,并且编译器警告不明确的赋值操作。最后,警告信息被消除了。

支持使用“老式初始化”

int x 1;

(没有等号)被类似的策略删除。这有助于解析器产生更智能的语法错误诊断。

可以预见的是,一些 C 用户忽略了这些警告,直到引入不兼容的编译器迫使他们在更改过时的源代码或维护自己的编译器版本之间做出选择。但总体来说,阶段性变革的策略是成功的。


Also, in
Brian W Kernighan and Dennis M Ritchie
The C Programming Language, 1st Edn (1978), on p212 in Appendix A, §17 Anachronisms, it says:

早期版本的 C 使用 =op 形式代替 op= 进行赋值运算符。这会导致歧义,典型的是:

<前><代码>x=-1

实际上会递减x,因为=-是相邻的,但它可能很容易意味着分配-1x

You can find evidence of the old notation in the 7th Edition UNIX Manual (Vol 2a) dated January 1979, available online at http://cm.bell-labs.com/7thEdMan/ (unavailable since approximately July 2015; the June 2015 version is now available via the WayBack Machine at http://cm.bell-labs.com/7thEdMan/ — or at https://9p.io/7thEdMan/).

The chapter is titled 'C Reference Manual' by Dennis M. Ritchie, and is in the PDF version of the manual, but not in the HTML version.
In the relevant part, it says:

7.14.1 lvalue = expression

The value of the expression replaces that of the object referred to by the lvalue. The operands need not have the
same type, but both must be int, char, float, double, or pointer. If neither operand is a pointer, the assignment
takes place as expected, possibly preceded by conversion of the expression on the right.
When both operands are int or pointers of any kind, no conversion ever takes place; the value of the expression
is simply stored into the object referred to by the lvalue. Thus it is possible to generate pointers which will cause addressing
exceptions when used.

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 =op E2’’ may be inferred by taking it as equivalent to
‘‘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.


Separately, there is a paper 'Evolution of C' by L Rosler in the 'UNIX® SYSTEM: Readings and Applications, Volume II', originally published by AT&T as their Technical Journal for October 1984, later published in 1987 by Prentice-Hall (ISBN 0-13-939845-7). One section of that is:

III. Managing Incompatible Changes

Inevitably, some of the changes that were made alter the semantics of existing valid programs. Those who maintain the various compilers used internally try to ensure that programmers have adequate warning that such changes are to take effect, and that the introduction of a new compiler release does not force all programs to be recompiled immediately.

For example, in the earliest implementations the ambiguous expression x=-1 was interpreted to mean "decrement x by 1". It is now interpreted to mean "assign the value -1 to x". This change took place over the course of three annual major releases. First, the compiler and the lint program verifier were changed to generate a message warning about the presence of an "old-fashioned" assignment operation such as =-. Next, the parsers were changed to the new semantics, and the compilers warned about an ambiguous assignment operation. Finally, the warning messages were eliminated.

Support for the use of an "old-fashioned initialization"

int x 1;

(without an equals sign) was dropped by a similar strategy. This helps the parser produce more intelligent syntax-error diagnostics.

Predictably, some C users ignored the warnings until introduction of the incompatible compilers forced them to choose between changing their obsolete source code or assuming maintenance of their own versions of the compiler. But on the whole the strategy of phased change was successful.


Also, in
Brian W Kernighan and Dennis M Ritchie
The C Programming Language, 1st Edn (1978), on p212 in Appendix A, §17 Anachronisms, it says:

Earlier versions of C used the form =op instead of op= for assignment operators. This leads to ambiguities, typified by:

x=-1

which actually decrements x since the = and the - are adjacent, but which might easily be meant to assign -1 to x.

乙白 2024-12-13 13:09:09

它只是赋值后跟一元加。

#include <stdio.h>
int main() {
    int a;
    a =+ 5;
    printf("%d\n",a);
    return 0;
}

打印“5”。将 a =+ 5 更改为 a =- 5 并打印“-5”。更简单的读取 a =+ 5 的方法可能是 a = +5

It's just assignment followed by unary plus.

#include <stdio.h>
int main() {
    int a;
    a =+ 5;
    printf("%d\n",a);
    return 0;
}

Prints "5". Change a =+ 5 to a =- 5 and it prints "-5". An easier way to read a =+ 5 is probably a = +5.

一梦浮鱼 2024-12-13 13:09:09

它是 += 的古老变体。在现代编译器中,这相当于一个赋值运算符后跟一个一元 +

It's an ancient defunct variant of +=. In modern compilers, this is equivalent to an assignment operator followed by a unary +.

<逆流佳人身旁 2024-12-13 13:09:09

我认为

a =+ 5;

应该等同于

a = (+5);

因此是风格非常糟糕的代码。

我尝试了以下代码并打印了“5”:

#include <iostream>
using namespace std;

int main()
{
    int a=2;
    a =+ 5;
    cout << a;
}

I think

a =+ 5;

should be equivalent to

a = (+5);

and therefore be code of very bad style.

I tried the following code and it printed "5":

#include <iostream>
using namespace std;

int main()
{
    int a=2;
    a =+ 5;
    cout << a;
}
柳絮泡泡 2024-12-13 13:09:09

读完你的问题后,我刚刚调查了这些。让我告诉你我发现了什么。在 gcc 和turboc 上尝试过。没有在 Visual Studio 上确定,因为我还没有在我的 PC 上安装它,

  int main()
  { 
   int a=6;
   a =+ 2;
   printf("%d",a);
  }  o/p , a value is 2

  int main()
  {
   int a=6;
   a =- 2;
   printf("%d",a);
  } o/p , a value is -2 

我不知道其他答案,因为他们说它是 C 的古老版本。但是现代编译器将它们视为要分配的值(这是正数或仅此而已),下面的代码让我更加确定这一点。

  int main()
  { 
   int a=6;
   a =* 2;  \\ Reporting an error inavlid type of argument of unary *
   printf("%d",a);
  } 
 if *= is equal to =* then it should not report error but its throwing an error

After reading your question I just investigated on these. Let me tell you what I have found. Tried it on gcc and turboc. Did not make it sure on Visual Studio as I have not installed it on my pC

  int main()
  { 
   int a=6;
   a =+ 2;
   printf("%d",a);
  }  o/p , a value is 2

  int main()
  {
   int a=6;
   a =- 2;
   printf("%d",a);
  } o/p , a value is -2 

I dont know about the other answers as they said its an ancient version of C.But the modern compilers treat them as a value to be assigned ( thats positive or negative nothing more than that) and these below code makes me more sure about it.

  int main()
  { 
   int a=6;
   a =* 2;  \\ Reporting an error inavlid type of argument of unary *
   printf("%d",a);
  } 
 if *= is equal to =* then it should not report error but its throwing an error
妄想挽回 2024-12-13 13:09:09

使用“=+”您只是将操作数指定为正数
例如 int a = +10;与负数相同 int a = -10;

using "=+" you are just assigning the operand is positive
for example int a = +10; same as for negative number int a = -10;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文