Need help trying to grasp 'typedef' and 'enum'

Since these keywords(?) are not in the Arduino Reference (not orange colour in the IDE) they are in C/C++ language. Have browsed some tutorials and can understand them until i caught something from Nick Gammon's superb reference website and that is;

// Example state machine reading serial input // Author: Nick Gammon // Date: 17 December 2011

// the possible states of the state-machine typedef enum { NONE, GOT_R, GOT_S, GOT_G } states;

// current state-machine state states state = NONE;

From a tutorial, i understand how enum works;

  enum colors_t {black, blue, green, cyan, red, purple, yellow, white};

and that is colors_t becomes a variable* (of int type) which can have the values of said colors (being the values of 0,1,2,3,4,5,6,7 - ie. enumerated as such) (Not variable but **variable type* !)

Also i get that typedef is for creating aliases to existing types like byte, char, int, etc.

...

OK - i think i get it now after realising that colors_t is a type, not a variable. That explains;states state = NONEto mean define state as a variable [u]of[/u] type stateswith initial value "NONE"

And then, that means;typedef enum {  NONE, GOT_R, GOT_S, GOT_G } states;declares "states" as a type-alias of "enum{NONE,etc...}" which i'm assuming is implicit as an integer ?

Have i got that right ?

enum and typedef are C language features. Buth are carried into C++, which is that programming language for the Arduino. There are some subtle differences that cause us old-timers to not always use the C++ way of doing things. The C way si still fully compatible.

enum colors_t {black, blue, green, cyan, red, purple, yellow, white};

is C++'s way of defining an enum. and giving it a user-friendly name. It is NOT valid C syntax.

typedef enum { NONE, GOT_R, GOT_S, GOT_G } states;

is how you define an enum in C (the enum statement and the values in the {}) and give it a user friendly name (the typedef keyword and the name on the end).

They are, in a C++ program, completely interchangeable. They are not, in a C program, interchangeable.

which i'm assuming is implicit as an integer ?

That's the only part of your summary that you should not be doing. Yes, the base type is int, but the whold purpose of using an enum (with name) is to force code to limit values to a defined subset of values).

Passing an enum to a function that takes a reference to int is possible, but it allows the function to assign to the int a value that is not in the range defined by the enum statement, which can cause all kinds of grief.

Use enums where appropriate. Use ints where appropriate. Do not mix them.

ok, thanks a lot. Nick Gammon's site looks a real valuable place for well-explained references, but real n00bs like me have to beware the subtle syntax of keywords(?) not in the Arduino IDE.

retronet_RIMBA1ZO: real n00bs like me have to beware the subtle syntax of keywords(?) not in the Arduino IDE.

The basic language syntax is C++ for Arduino sketches and .cpp files you use, and 'C' for any .c files you use. C++ and 'C' are similar and very widely documented and there are numerous online tutorials to teach you how to use them.

The Arduino includes AVR's implementation of the 'C' runtime library. This adds a set of utility functions to the basic language. This gives you a large and very powerful set of utilities including, for example, string processing. The Arduino reference page has a link to AVR's documentation for their runtime library.

Finally you have the Arduino runtime library, which provides a set of Arduino-specific functions to access the hardware, and a few utilities that the Arduino developers decided to include to make your code simpler.