What does the C ??!??! operator do?!
原帖: http://stackoverflow.com/questions/7825055/what-does-the-c-operator-do
===============================================================================================================================
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?
=================================================================================================================
??!
is 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.
=================================================================================================================
A very common user terminal from the initial Unix and C era was the ASR-33 Teletype.
This device was slow (10 cps) and noisy and ugly, and it’s view of the ASCII character set ended at 0x5f, so it had no
{ | } ~
The trigraphs were defined so that C programs could use the ASCII subset found on the ASR-33 and in other environments missing the high ASCII values.
=================================================================================================================
t’s a C trigraph :
http://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C
??!
being |
so this is operator ||
=================================================================================================================
还没有评论,来说两句吧...