tags:

views:

336

answers:

6

I remember reading a book talking about standard the C programming language. It said in some kinda C you can write i=+1 which equals i+=1. So i(operator)=(expression) equals i=(operator)(expression). I never see this kind of C, is there someone who can explain this?

Best regards and thanks, Fan

+21  A: 

That was the very very inital syntax for the += operator in C. I think it was deprecated even before the first edition of K&R book.

Remo.D
Would you site a reference for this?
The C Programming Language, 2nd Ed. Appendix C, p. 260, "The old assignment operators like =+ are truly gone."
plinth
Remo.D
Also interesting to note: back then, C didn't have a unary + operator.
Richard Pennington
I added a quote from K` to initialize `x` to `1`?! I wish I had a copy of the first edition!
Alok
+1  A: 

This is not C. In standard C that is equivalent to i=1. Might it be that you are looking for i++?

Nikolai N Fetissov
+7  A: 

Kernighan and Ritchie's The C Programming Language, first edition explains this. I found a quote in this post on comp.lang.c. Relevent part of the quote:

Since C is an evolving language, certain obsolete constructions may be found in older programs. Although most versions of the compiler support such anachronisms [--as of 1978--], ultimately they will disappear, leaving only a portability problem behind.

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 intended to assign -1 to x.

Wikipedia also has a similar description. From their entry for C, talking about K&R C and "Old C" differences:

compound assignment operators of the form =op (such as =-) were changed to the form op= to remove the semantic ambiguity created by such constructs as i=-10, which had been interpreted as i =- 10 instead of the possibly intended i = -10.

Alok
+1. Yay for Wikipedia quotes!
jdmichal
You make me sad. I had the first edition. I think I lent it to my son. I wonder if he still has it?
Richard Pennington
Fred Larson
@Fred, thanks. @Richard, I just ordered a copy of the first edition from an online store!
Alok
A: 

It would most definitely give issues when you want to assign a negative number to a variable. What decides what you mean then? Spacings? (ew!) Or parenthetis (double ew!). Here are some examples showing the issues

i = -1; //Is this any different from the line below? Since when have spaces in these kind of cases mattered?
i =- 1; //If the suggested syntax existed, what would these two lines mean?

//The only thing left now (if we rule out making spaces matter) is to use parenthetis in my eyes, but...

i =(-1); //This is just ugly

As pointed out in the comments, the * symbol which is used to dereference pointers presents the exact same issue as the minus sign.

kastermester
I'm pretty sure C was designed to be whitespace-agnostic. So `i = (-1)` would be the best way to distinguish them. Though I could see the argument that `=-`, as an operator, is an atomic unit and can't have whitespace inserted without changing the meaning accordingly.
jdmichal
I know - I was merely trying to provide examples why a such syntax would really just make no sense at all. But maybe I should've made that more clear.
kastermester
@jdmichal: C is not really anywhere close to "whitespace-agnostic". For an obvious example, consider `a=x / *y;` vs. `a = x /* y;`. The first divides `x` by whatever `y` points at. The second has `a=x` followed by the beginning of a comment.
Jerry Coffin
@Jerry: It was agnostic in this case. Originally, in "=+", the "=" and "-" were separate tokens.
Richard Pennington
+12  A: 

This is true. That version of C is called CRM C (CRM stands for "C Reference Manual" - a document written by Dennis Ritchie). There are many weird things in that version of C.

You can download the document here http://cm.bell-labs.com/cm/cs/who/dmr/cman.pdf

AndreyT
That's it! I had it printed in a book that was part of the Digital Manual set.
Remo.D
Thanks very much !
Weixiao.Fan
A: 

A little bit of whitespace might help clarify this for you.

When you write i=+1, what is actually happening is i = +1. This is because there is no =+ operator in C. The = is treated as its own operator, and the + is a unary operator acting on the constant 1. So the evaluation of this statement starts on the right hand side of the = operator. +1 will evaluate to 1 and the = operator will then assign that value to the variable i.

+= is it's own operator in C, which means "add the value of the expression on the right side of this operator to the variable on the left side, and assign it to that variable, so something like the following:


i = 3;
i += 2;

would evaluate to 5 because the += operator will evaluate the right side of the operator (in this case 2, and will add it to the left side (in this case, i has a value of 3) and will assign it to the variable on the left. In essence, this becomes i = (2 + 3). Therefore, the variable i will have a final value of 5.

If you're just adding the value 1 to an integer variable, you can use the ++ operator instead. Adding this operator after a variable (i.e. i++) will cause the current line of code to execute before incrementing the variable by one. Prefixing the operator in front of the variable will cause the statement to be executed after the variable is incremented.

e.g.:


i = 5;
j = i++;

will result in i = 6 and `j = 5', whereas


i = 5;
j = ++i;

will result in i = 6 and j = 6.

Similarly, the -- operator can be used to decrement (decrease) the variable by one, similar to how ++ will increment (increase) the variable by one. The same rules regarding positioning the operator before or after the variable apply to both operators.

Hope this clears things up a bit.

AgentConundrum