Can you have a triple minus signs in C programming? What does it mean?

C++C

C++ Problem Overview


> 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.

C++ Solutions


Solution 1 - C++

It is equivalent to:

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

This is in accordance with the maximal-munch principle

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

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.

Solution 5 - C++

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 :-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJoseph LeeView Question on Stackoverflow
Solution 1 - C++RobᵩView Answer on Stackoverflow
Solution 2 - C++imrealView Answer on Stackoverflow
Solution 3 - C++Olaf DietscheView Answer on Stackoverflow
Solution 4 - C++JeyaramView Answer on Stackoverflow
Solution 5 - C++humblecoderView Answer on Stackoverflow