views:

157

answers:

2

This is the only place I've ever seen and, or and not listed as actual operators in C++. When I wrote up a test program in NetBeans, I got the red underlining as if there was a syntax error and figured the website was wrong, but it is NetBeans which is wrong because it compiled and ran as expected.

I can see ! being favored over not but the readability of and && or seems greater than their grammatical brothers. Why do these versions of the logical operators exist and why does seemingly no one use it? Is this truly valid C++ or some sort of compatibility with C that was included with the language?

+16  A: 

They originated in C in the header iso646.h. There were keyboards which couldn't type the symbols needed, so the header made #define's that would assist them. Of course, as time went by this became less used.

In C++, they became what's known as alternate tokens. You need not include anything to use these tokens in a compliant compiler. Alternate tokens are just like regular tokens, except for spelling. So and is exactly the same as &&, it's just a different way of parsing the same thing.

For their use, because they are rarely used it's often more surprising and confusing to use them than it is helpful. I'm sure if it were normal, they would be much easier to read, but people are so used to && and || anything else just gets distracting.

GMan
+1 Very nice answer, with explaining the rationale. :-) (Me, I was just going for Fastest Gun in the West. :-P)
Chris Jester-Young
So is the interpretation of these alternate tokens just a compiler feature or is it in the C++ specification?
Kavon Farvardin
@Kavon: It's specified in section 2.5 of the standard; it's a language feature.
GMan
I personally think they are much better... but then I am Python biased. I don't know why some people thing that if it's not garbled it's not code...
Matthieu M.
+4  A: 

In C++, they are real keywords. In C, they're macros defined in <iso646.h>. See http://www.dinkumware.com/manuals/?manual=compleat&amp;page=iso646.html.

Chris Jester-Young
Technically they are alternate tokens, not keywords. :)
GMan
@GMan: +1 Nice call, though, the Dinkumware page calls them keywords, so, I suppose they didn't care too much about the distinction.
Chris Jester-Young