I use Bloodshed Dev-C++ version and it is a little weird in that it accepts "and" as "&&" and "or" as "||", not to mention not requiring parentheses around separate clauses in and/or connected statements. Therefore it would read the following as the same.

if(voice==loud or voice==annoying and voice==whiny){
cout<<"SHUT UP!"<<endl;
}

if((voice==loud) || ((voice==annoying) && (voice==whiny))){
cout<<"SHUT UP!"<<endl;
}

Strange, huh?
Venomlash

Dani AI

Generated

Your observation is exactly what C++ allows: compilers that implement the standard will accept the English spellings and, or, not, bitand, bitor, etc., as alternative operator tokens that behave the same as &&, ||, !, &, |, and so on. That explains why your Dev-C++ build (GCC/MinGW) accepts those words without extra syntax, and why the expression you posted parses without extra parentheses — the language defines those tokens explicitly. (cppreference.com)

A few clarifications worth adding to the thread: the correct C header name is <iso646.h> (not the mistyped ios646.h), and historically there has also been a C++ compatibility header <ciso646>. In strictly conforming C++ the words are language tokens so the header does nothing; <ciso646> was a compatibility wrapper and was removed from the standard library in C++20 (the plain C <iso646.h> remains for C). This corrects the slightly mixed advice in earlier replies. (en.cppreference.com)

Portability advice: if the code needs to compile as C or with older/nonconforming toolchains (older MSVC, embedded toolchains, etc.), prefer the symbol forms && and || or explicitly include the appropriate header for C (and, for some old MSVC setups, the compatibility header or a standards mode flag). Modern MSVC supports the words in standards-compliant modes, but older defaults sometimes required the header or special flags — so using symbols is the safest cross-compiler choice. This is the pragmatic point behind ’s recommendation. (stackoverflow.com)

On precedence and readability: == binds tighter than logical AND, and && binds tighter than ||, so your code is parsed the way you expected; nevertheless, adding parentheses improves clarity for future readers and avoids reliance on remembering precedence rules. For reference, see the C++ operator-precedence table. (en.cppreference.com)

Recommended Answers

All 13 Replies

It's not an "abnormality," it's actually part of the C++ standard. As I understand it, operators like 'and' and 'or' are included for users with different character sets. See: http://david.tribble.com/text/cdiffs.htm#C99-alt-tok

Microsoft compilers appear to implement these as macros instead of keywords in the ciso646 header.

As for the second part of your post, the equality operator takes precedence over logical AND, which takes precendence over logical OR, so the parentheses aren't required in either case.

commented: Good comment - Salem +3
commented: smart +5
Member Avatar for Member #46692

Best stick with && instead of "and" as you'll encounter problems if you switch compilers.

commented: narrow thinking -1

Best stick with && instead of "and" as you'll encounter problems if you switch compilers.

Again, it depends on which country you live in. Not all keyboards have a & key!!!! Actually "and" looks a lot more readable than "&&". Dev-C++ uses a much more international open source GNU compiler. Thank you GloriousEremite for the reference site!

Member Avatar for Member #46692

I think he does have the & key. So therefore he should use it -if he wants to guarantee better portability.

If not I would expect him to have a little note saying, if you intend to use this code with such and such a compiler please include the relevant header.

>Not all keyboards have a & key!!!!
Can't ya just hold down [alt] + ### or whatever the number is for an ampersand?

Again, it depends on which country you live in. Not all keyboards have a & key!!!! Actually &quot;and&quot; looks a lot more readable than &quot;&&&quot;. Dev-C++ uses a much more international open source GNU compiler.

As expected from a Python proponent :D But seriously, i agree with Mr. Iamthwee, its would be better NOT to use such keywords which are fully integrated in C++ standards but require the inclusion of a seperate header file #include <iso646.h> . So if you want your C++ code to be compatible with C standards, better stick to the && operator.

C90 does not have these built-in keywords, but it does provide a standard header file that contains definitions for the same words as macros, behaving almost like built-in keywords. The recommended practice for code intended to be compiled as both C and C++ is to use &quot;and&quot; identifiers only for special meanings, and only after including the header <iso646.h> .

ios646.h is apparently available in the C99 C standards. But I don't know how many compilers have implement those new C standards yet.

ios646.h is apparently available in the C99 C standards. But I don't know how many compilers have implement those new C standards yet.

I use Code::Blocks IDE which comes packed with the GCC Mingw compiler and the C File created in it does not recognize the C++ keywords.

Member Avatar for Member #46692

I use turbo c which crashes most of the time.

The C compiler that comes with Dev-CPP must be more modern ...

// using "and" and "or" rather than "&&" and "||"
// as a Dev-Cpp  C project

#include <stdio.h>
#include <iso646.h>  // yes this is needed!

int main()
{
  int loud, annoying, whiny;
  int voice = 1;
  
  loud = annoying = whiny = 1;
  
  if (voice==loud or voice==annoying and voice==whiny)
  {
    printf("SHUT UP!\n");
    //cout<<"SHUT UP!"<<endl;
  }
  getchar();  
  return 0;
}

Turbo C??? I thought that was pretty bad 20 years ago!

Turbo C??? I thought that was pretty bad 20 years ago!

At one time it was one of the best MS-DOS compilers on the market -- Microsoft compilers really sucked cannel water in those days and NOBODY used their IDE. That was when Lattice C was king of the hill.

Again, it depends on which country you live in. Not all keyboards have a & key!!!! Actually "and" looks a lot more readable than "&&". Dev-C++ uses a much more international open source GNU compiler. Thank you GloriousEremite for the reference site!

Please tell me you're kidding about the keyboards lacking an ampersand! I have never heard of that. Just to connect this to something funny, has anyone seen the Dilbert strip in which the company accidentally ships keyboards lacking the letter Q, marketing them under the slogan "The Big Q of Quality"? Bizarre, eh?
Venomlash

Well, it isn't really a good thing that and and && are the same to Dev, but it isn't really a problem if you just follow the rules of C++, now is it?

And even Visual Studio doesn't care if there are parentheses around independant clauses, but once again it's just good coding, so it shouldn't cause any problems.

The & is missing from keyboards in a number of European countries, as is the $ symbol. They however have other symbols they need on their keyboards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.