Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Matthew Wilson

Dr. Dobb's Bloggers

Watch out for operator alternates

September 14, 2010

I'm doing some examples for the next instalment of my Quality Matters column for ACCU's Overload, and got caught by a head-scratcher that someone who's been programming C and C++ for as long as I have has no right to forget. See if you can spot the problem.

At the moment, I'm writing a set of instalments examining exceptions. One of the issues I'm covering is under what conditions it's possible to assert that a program must succeed, as opposed to those in which success may be assumed for the majority of conditions, or those in which success may not be assumed and contingent action taken. I give a couple of examples of programs that can be asserted, by inspection, to never fail, and for which it is therefore reasonable to return 0.

Author diligence - read "paranoia" and "experience", having been caught out with embarassing syntax errors in published works before - compelled me to compile them, even though I knew they were right: they were so simple, after all. Well, of course, I was caught out. When compiling the following code, Borland, Visual C++ and CodeWarrior were all happy as a dog with a stick. GCC, however, gave me a weird, and temporarily discombobulating, compile error: ..\hello-world.4.c:2: error: expected unqualified-id before '!' token

/* hello-world.4.c */
<strong>int not(int v)</strong>
  return !v;
int main()
  return not(1);

The way I tend to look at compile errors is to look at the line, before even considering the nature of the error. So, I looked at the line, which is obviously ok. Then I considered the details of the error, which pertain to the ! symbol, but that doesn't appear until line 4. Hmmm. Confusion.

What caused me to "get it" within a minute or two was the fact that with all other compilers I tried (incl. Borland, CodeWarrior, and Visual C++) it all worked fine. So it must be something GCC. And then I remembered having hit this before, though with and rather than not.

What's happpening here is that GCC is being more eager in tis standards-compliance than the others in this respect. The C standard specifies (in clause 7.9) a number of alpha-only symbols that are to be replacements for various logical and bitwise operators, to account for programming environments where the operator symbols are not readily available. The symbols are shown in the following table.

and  &&
and_eq  &=
bitand  &
bitor  |
compl  ~
not  !
not_eq  !=
or  ||
or_eq  |=
xor  ^
xor_eq  ^=

The requisite #defines reside within the standard header iso646.h. With the other compilers, you have to include them explicitly in order to encounter the conflict. GCC is just being more eager, in righteously policing the use of symbols that are reserved whether you declare you want to use them or not. And that's a good thing. Yet another reason why you should always try to compile your C and C++ code with multiple compilers.

Out of interest, if you wish to verify this with Visual C++, save the above program code to hello-world.4.c and then execute the command-line:

  cl -FIiso646.h -nologo hello-world.4.c

The -FI option forces the inclusion of the named header, which can be a neat thing from time to time.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.