From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/enums.txt | 55 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/enums.txt (limited to 'reference/C/CONTRIB/SNIP/enums.txt') diff --git a/reference/C/CONTRIB/SNIP/enums.txt b/reference/C/CONTRIB/SNIP/enums.txt new file mode 100755 index 0000000..ae88298 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/enums.txt @@ -0,0 +1,55 @@ +Some of my favorites... +----------------------- + +typedef enum {ERROR = -1, SUCCESS, FALSE = 0, TRUE} logical; +#define BOOL(x) (!(!(x))) /* always TRUE or FALSE */ + +/* (trivial) Example code follows */ + +#define MAX_VAL 10000 /* data upper bound */ +#define MIN_VAL -37 /* data lower bound */ + +logical testfunc(int intvalue) +{ + if (MAX_VAL < intvalue || MIN_VAL > intvalue) + return ERROR; /* if out of bounds */ + else return BOOL(intvalue); /* zero returns FALSE, + anything else is TRUE*/ +} + +/* Examples using SUCCESS/ERROR */ + + if (SUCCESS == strcmp(my_string, "something")) + do_something(); + if (ERROR == open("my_file", O_READ)) + abort(); + + And, speaking of enumerated data types (which we can feel free to do +since they're now "official" with the adoption of ANSI C), these are very +handy when defining lists of data which may need to be appended in the +future. If you define your enums this way: + +enum CARS {CARS_MIN = -1, FORD, CHEVY, PLYMOUTH, CARS_MAX}; + +...then you can write "expandable" code as follows: + +logical real_car(enum CARS my_car) +{ + if (CARS_MIN >= my_car || CARS_MAX <= my_car) + return FALSE; + else return TRUE; +} + +By including `CARS_MIN' and `CARS_MAX' as dummy enumerations, you can +change the declaration to: + +enum CARS {CARS_MIN = -1, FORD, CHEVY, PLYMOUTH, FERRARI, CARS_MAX}; + +...and all your existing code will still work properly, allowing you to +spend your time writing new code to support the new enumerations rather +than going back to fix any bounds checking you may have already written. +In addition if, within the enum declaration, you declare `CARS_MIN = -1', +then you can also include this handy little bit of expandable code: + + printf("Right now, I know about %d type%s of CARS\n", CARS_MAX, + &"s"[1 == CARS_MAX]); -- cgit v1.2.3-54-g00ecf