and, &&, - since when did 'and' work?

Hello,

I was just writing some very very basic code to show a mate who hasnt programmed a line of code in his life, how some simple code can make sense.

Istarted writing a few lines to try and show what compile errors are, and expecting to get a compile error - I didnt.

I wrote this - the code wasnt even complete...

int Test;
int Button;
int Boat;
int Output;

void setup()
{
  Serial.begin(9600);  
}

void loop()
{
  if(Test == 1 && Button == 2 and Boat == 3)
  {
    Output = 1;
  }
}

It compiled. ??

I was not expecting 'and' to compile in place of a &&

Well it makes sense.

I can't quite remember about 'and', I have a vague recolection that it doesn't do what && does but I can't remember.

Well this is creepy:

This file:

arduino-0022\hardware\tools\avr\lib\gcc\avr\4.3.2\include\iso646.h

Contains:

#ifndef __cplusplus
#define and	&&
#define and_eq	&=
#define bitand	&
#define bitor	|
#define compl	~
#define not	!
#define not_eq	!=
#define or	||
#define or_eq	|=
#define xor	^
#define xor_eq	^=
#endif

Well that's nice but I'd much rather stick to &&, || etc

From the name of the file ("iso546.h") it appear the the intent is to provide an alternative for those ISO 646 7-bit ASCII characters sets that do not contain some of the punctuation marks used in C and C++. Not necessary, of course, because the C standard has its own way of dealing with those characters:

(I'm a bit surprised that __cplusplus is NOT defined during an Arduino compile.)

[quote author=Nick Gammon link=topic=60082.msg433173#msg433173 date=1304334410]
Well this is creepy:[/quote]

Well, creepy is as creepy does, but that's not really creepy. That file and the definitions therein are part of the C standard language specification ISO/IEC 9899:1999. (Page 201, paragraph 7.9.)

As you noted, the stuff in that file is defined that way for C programs that include that header. What about C++? Well, that is covered in the C++ standard language specification ISO/IEC 14882:2003. That set of identifiers is listed as reserved. (Table 4 - alternative representations on Page 14.) They are part of the C++ language and do not depend on macro definitions.

Regards,

Dave

johnwasser:
...
(I'm a bit surprised that __cplusplus is NOT defined during an Arduino compile.)

In fact when avr-gcc is invoked as a C++ compiler (it's automatic when the file is named something.cpp), __cplusplus is defined. Arduino sketches end up in a file named something.cpp and the compile line is avr-g++ withabunchofstuff something.cpp andabunchofotherstuff

void setup()
{
    Serial.begin(9600);
    #ifdef __cplusplus
    Serial.print("__cplusplus is defined.");
    #else
    Serial.print("__cplusplus is not defined.");
    #endif
}
void loop(){}

Output:


__cplusplus is defined.

Regards,

Dave

C++ Keywords - ANSI (C++ 99)

Well known      Alternate
-------------------------
[               ??(
]               ??)
{               ??<
}               ??>
|               ??!
\               ??/
#               ??=
^               ??'
~               ??-
{               <%
}               %>
[               <:
]               :>
#               %:
##              %:%:
&&              and
|               bitor
||              or
^               xor
~               compl
&               bitand
&=              and_eq
|=              or_eq
^=              xor_eq
!               not
!=              not_eq

asm
bool
catch
class
compl           ~
const_cast
delete
dynamic_cast
explicit
false
friend
if
inline
mutable
namespace
new
operator
private
protected
public
reinterpret_cast
static_cast
template
this
throw
true
try
typeid
typename
using
virtual

Well I didn't know that. I tried this under "normal" C++ (not under Arduino) and it compiles OK:

int main ()
{
int a = 1 and 2;
return 0;
}

So it doesn't rely on that include file (compiling under C++).

lloyddean:
C++ Keywords - ANSI (C++ 99)

Can you give the exact reference? I am not familiar with "ANSI (C++ 99)."

However...

Comparing your post with Table 3 in section 2.11 on page 14 of Programming Languages --- C++, ISO/IEC 14882:2003(E), I perceive that your list is missing the following 33 reserved keywords:


auto
break
case
char
const
continue
default
do
double
else
enum
export
extern
float
for
goto
int
long
register
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
wchar_t
while

Regards,

Dave

No i can't as I put it together years ago, but you're correct in that they are missing from my list as posted.

I seem to have a problem, somewhere, when I copy from my system to the forums editor, where content disappears or is wrongly formatted. I wish I new exactly where it was happening. It most often looks correct in the "preview" but occasionally is whacked durning the "post" process.

My apologies for the incomplete list.

Thanks for all the posts - very interesting.

I was expecting to get a compile issue.
I was sure I had done this in the past and it didnt work - must have been a few releases ago...

Thanks

Thanks very much for the postings also.

Arduino is a marvellous thing, especially for those either new to programming, or advancing from other controllers that didn't use c/c++.

Now if someone was to include these into the official reference pages.....

Thanks
Mark