59

Possible Duplicate:
Why doesn’t a+++++b work in C?

I got this from page 113 on the "An Embedded Software Primer" by David Simon.

I saw this statement below:

iHoursTemp = iHoursTemp + iZoneNew ---iZoneOld;

Can you really have three minus signs in this line? What does a triple minus sign mean?

I believe it is a C programming statement.

Community
  • 1
  • 1
Joseph Lee
  • 529
  • 1
  • 5
  • 10
  • 58
    You can if you know how to survive the wrath of your teammates. – R. Martinho Fernandes Dec 14 '12 at 07:08
  • to check legitimation, use a compiler, he knows more than us. to check what it does, test I/O on statments like that. – elyashiv Dec 14 '12 at 07:08
  • 8
    @elyashiv don't overly rely on simple test results, though. Undefined behaviour lurks at every corner. – R. Martinho Fernandes Dec 14 '12 at 07:09
  • 10
    Yet another question that deserves the "Tricky Question" badge - for baiting multiple downvoted answers. – Mysticial Dec 14 '12 at 07:14
  • 1
    @Mysticial, Not to mention 11 upvotes on a similar comment :p – chris Dec 14 '12 at 07:16
  • 7
    Wait a minute, this was in a book with no explanation? Oh boy! – chris Dec 14 '12 at 07:28
  • 5
    Sounds like yet another [bullschildt](http://catb.org/jargon/html/B/bullschildt.html) book. – Lundin Dec 14 '12 at 07:51
  • 5
    Interesting fact: `iZoneNew --+--iZoneOld` compiles and does what is expected. `iZoneNew -----iZoneOld` however will not compile since the compiler interprets it as `((iZoneNew--)--) - iZoneOld`. – Leo Dec 14 '12 at 09:02
  • 2
    I think this is a typing error in the book. I can't believe anybody writing books with such ugly statements. – sorencito Dec 14 '12 at 09:52
  • 2
    What does it mean? It means the guy who wrote this code ought to be fired. – Tony the Pony Dec 14 '12 at 10:00
  • 1
    To be fair to the book’s author, the space after iZoneNew was added by the OP, the book just [says](http://books.google.ch/books?hl=de&id=xG2ZD55_BJAC&q=iZoneNew#v=snippet&q=iZoneNew&f=false) `iHoursTemp = iHoursTemp + iZoneNew---iZoneOld;` (which isn’t much better, but at least a little bit). – Raphael Schweikert Dec 14 '12 at 10:25
  • 3
    This is closely related to the "approaches operator", `-->`, as used in `while (x --> 0)`. – molbdnilo Dec 14 '12 at 10:45
  • 1
    @Joseph, I strongly suggest you click the checkmark next to your preferred answer to accept it. It's one way of saying thanks, and moves that answer to the top of the list. – chris Dec 14 '12 at 11:38
  • 1
    @rjayavrp No, it decrements `iZoneNew` (post decrement `x--`). – Jens Dec 14 '12 at 16:13
  • 1
    My god, this gets asked about once a month, and it always gets so many upvotes.... http://stackoverflow.com/questions/5341202 http://stackoverflow.com/questions/5677271 http://stackoverflow.com/questions/5752910 http://stackoverflow.com/questions/5649354 http://stackoverflow.com/questions/7485088 – BlueRaja - Danny Pflughoeft Dec 14 '12 at 17:06
  • 1
    One of the most disgusting use of C I've ever seen but what can you expect from code which uses Hungarian notation? – kamilk Dec 14 '12 at 17:49
  • -1. I hate these questions "what does the operator x do?" where x is '!!?++!??+&&&&+-+-+&-+&-+&+&-+-|-+-+&!&-&+-+-+&!-+|-+-!+&-+|=' – alexpinho98 Jun 30 '13 at 14:41

5 Answers5

95

It is equivalent to:

iHoursTemp = iHoursTemp + (iZoneNew--) - iZoneOld;

This is in accordance with the maximal-munch principle

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 9
    Finally, a completely correct answer :) – chris Dec 14 '12 at 07:13
  • 1
    Why would anyone want to program like the original sentence? Don't you think a modified statement like this is better and clearer for the computer? iHoursTemp = iHoursTemp - iZoneOld + (iZoneNew--); – Joseph Lee Dec 14 '12 at 07:23
  • 8
    @Joseph Lee Noone would ever want to program this. However its not the computer that has trouble with it (it knows the rules if you have a proper compiler), but it is people that will constantly fail to understand this. To be honest I wouldn't even do an inline increment, like the 'readable' version, unless performance absolutely doesn't allow to make it several statements. Because even if people can read what it means, really understanding it, is different. – Thirler Dec 14 '12 at 07:50
  • And yet "++++" is explicitly disallowed by syntax rules. I believe that "--" and "++" should also have been given "no-munch" clauses in the C++ standard. The lack of it was probably an oversight, and not a tacit condonation of this horrid coding style. – Warren P Dec 14 '12 at 17:17
  • 1
    @WarrenP - "++++" is not "*explicitly disallowed by syntax rules.*" Consider [this program](http://ideone.com/EsJVZC) – Robᵩ Dec 17 '12 at 14:46
49

The correct answer is (as Rob said) the following:

iHoursTemp = iHoursTemp + (iZoneNew--) - iZoneOld;

The reason it is that way and not

iHoursTemp = iHoursTemp + iZoneNew - (--iZoneOld);

is a convention known as maximum munch strategy, which says that if there is more than one possibility for the next token, use (bite) the one that has the most characters. The possibilities in this case are - and --, -- is obviously longer.

imreal
  • 10,178
  • 2
  • 32
  • 48
  • Why would anyone want to program like the original sentence? Don't you think a modified statement like this is better and clearer for the computer? iHoursTemp = iHoursTemp - iZoneOld + (iZoneNew--) – Joseph Lee Dec 14 '12 at 07:46
  • 1
    @JosephLee, yes it would be much better and easier to read. But the question stimulated a nice discussion about compiler strategies. – imreal Dec 14 '12 at 07:47
  • 1
    @Cthulhu, It's funny because Rob was the first to mention it, in a comment on a now-deleted answer. – chris Dec 14 '12 at 08:32
  • @JosephLee For the computer? What about human readers?!!!!!1 – unwind Dec 14 '12 at 08:32
  • Haha.. Wow, Did not know that. @chris I added the definition to his answer, to give credit where it is due. :) – Anirudh Ramanathan Dec 14 '12 at 08:36
  • 1
    I can't think of any situation where mixing -- or ++ operators with other operators in the same expression makes any sense. I can think of many situations where it confuses the reader and leads to undefined or unspecified behavior. So for the sake of the programmer themselves, as well as human readers, the proper way to fix this code is to move iZoneNew-- to a row of its own. – Lundin Dec 14 '12 at 08:37
12

According to Draft C++11 (PDF) 2.5 Preprocessing tokens, clause 3 and Draft C11 (PDF) 6.4 Lexical elements, clause 4, the compiler parses the longest possible sequence of characters as the next token.

This means --- will be parsed into the two tokens -- and -, which gives

iHoursTemp = iHoursTemp + (iZoneNew--) - iZoneOld;

This also shows, if you're unsure about precedence or parsing rules, use parenthesis to clarify the code.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • 3
    Only one of these interpretations is allowed. – R. Martinho Fernandes Dec 14 '12 at 07:17
  • @R.MartinhoFernandes There's nothing in the standard, that mandates either interpretation. So it's up to the implementation, which one it chooses. – Olaf Dietsche Dec 14 '12 at 07:34
  • 22
    Yes, there is. In C99 that is §6.4 Lexical elements, paragraph 4: "If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.". This is informally known as the maximal munch rule, as stated various times in comments and other answers here. – R. Martinho Fernandes Dec 14 '12 at 07:38
  • @R.MartinhoFernandes I must admit, I haven't looked into C, only C++. So, point taken. – Olaf Dietsche Dec 14 '12 at 10:38
  • 11
    In C++11 it is in §2.5 Preprocessing tokens, paragraph 3: "If the input stream has been parsed into preprocessing tokens up to a given character: [— rules for raw string literals] [— rules for `<::`] — — Otherwise, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token, even if that would cause further lexical analysis to fail." Seems you did not look hard enough. – R. Martinho Fernandes Dec 14 '12 at 10:48
  • 1
    OMG.. -15?! Too many downvotes – Anirudh Ramanathan Dec 14 '12 at 12:06
  • 1
    @Cthulhu Don't worry, they disagreed with the initial answer. I revised it since then. – Olaf Dietsche Dec 14 '12 at 12:09
  • 2
    17 down, 4 up - by my count that means @OlafDietsche is up 6 reputation on this answer. Share and enjoy :-) – Bob Jarvis - Слава Україні Dec 14 '12 at 13:14
  • 1
    Hopefully now the the incorrect information has been edited out, some people will reverse their votes. – Dan Is Fiddling By Firelight Dec 14 '12 at 13:54
  • 3
    Cool, kudos for not giving up with so many downvotes. I just reverted mine, and gave you a +1 :) – jalf Dec 15 '12 at 11:05
5

Equals to

iHoursTemp = iHoursTemp + (iZoneNew--) -iZoneOld;

#include <stdio.h>

int main()
{

int iHoursTemp = 2, iZoneOld = 3, iZoneNew = 4;

//2+4 - 2 = 4
iHoursTemp = iHoursTemp + iZoneNew ---iZoneOld;
//2+(4--) -3 = 3   

printf("\n :%d \n", iHoursTemp);

return 0;

}

Gives me 3 in gcc.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
-9

Sure why not. This statement

iHoursTemp = iHoursTemp + iZoneNew ---iZoneOld;

is equivalent to

iHoursTemp = iHoursTemp + iZoneNew -(--iZoneOld); //first decrement iZoneOld then perform rest of the arithmetic operation.

A little brain teaser, but fun to write :-)

humblecoder
  • 499
  • 2
  • 5
  • 15