Best operators questions in October 2011

What does the C ??!??! operator do?!

192 votes

I saw a line of C that looked like this:

!ErrorHasOccured() ??!??! HandleError();

It compiled correctly and seems to run ok. It seems to like it's checking if an error has occurred, and if it has, it handles it, but I'm not really sure what it's actually doing or how it's doing it. It does look like the programmer is trying express his feelings about errors.

I have never seen the ??!??! before in any programming language, and I can't find documentation for it anywhere. (Google doesn't help with search terms like ??!??!). What does it do and how does the code sample work?

It's a trigraph that translates to |. So it says:

!ErrorHasOccured() || HandleError();

which, due to short circuiting, is equivalent to:

if (ErrorHasOccured())
    HandleError();

Guru of the Week (deals with C++ but relevant here), where I picked this up.

Possible origin of trigraphs or as @DwB points out in the comments it's more likely due to EBCDIC being difficult (again). This discussion on the IBM developerworks board seems to support that theory.

From ISO/IEC 9899:1999 ยง5.2.1.1, footnote 12 (h/t @Random832):

The trigraph sequences enable the input of characters that are not defined in the Invariant Code Set as described in ISO/IEC 646, which is a subset of the seven-bit US ASCII code set.

What is x after "x = x++"?

69 votes

Possible Duplicate:
Is there a difference between x++ and ++x in java?
Why does this go into an infinite loop?

What happens (behind the curtains) when this is executed?

int x = 7;
x = x++;

I compiled and executed this. x is still 7 even after the entire statement. In my book, it says that x is incremented!

x = x++;

is equivalent to

int tmp = x;
x++;
x = tmp;