Home » C++ » Can you have a triple minus signs in C programming? What does it mean? [duplicate]

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

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Answers:

It is equivalent to:

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

This is in accordance with the maximal-munch principle

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

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 🙂