TRUE and FALSE

Tim Bomgardner bomgard at copper.ucs.indiana.edu
Fri Aug 31 03:16:53 AEST 1990


In article <1990Aug29.153917.28110 at warwick.ac.uk> asrap at warwick.ac.uk (Sean Legassick) writes:
>[...]
>	And how about some more:
>
>	#define EQUALS ==
>	#define LESSTHAN <	...... ad infinitum

You're gonna laugh, but I never got Fortran out of my system (so to
speak).  After a couple coding errors early in my C career of the form 

  if (x = y) ...  and
  if (x << y) ...

I put the following defines in a .h file:

  #define EQ ==
  #define NE !=
  #define LT <
  #define NOT !
  #define AND &&
  #define OR ||
  etc.

Another guy I know went even further, with defines such as

  #define THEN {
  #define ELSE } else {
  #define ENDIF }
  etc.

It actually made converting a lot of old fortran programs to C go pretty
quickly.

>	Okay, so maybe I'm going over the top a little - but you get my point.
>Personally I go further than simply using TRUE and FALSE (which by the way
>I always define as (1==1) and (1==0), not for portability but for a little
>extra clarity and any compiler worth its salt will optimise them down anyway).
>	I have in a standard header file a typedef for bool (which I typedef
>to an int, although if I used ANSI extensions I could add even more clarity
>and typedef to an enum { false, true }. This I find is invaluable for
>clarities sake. An int declaration says to me this variable is going to hold
>a scalar value - which is very different from a boolean one.
>	I would be interested in anyone who thinks an int declaration of
>a boolean variable is clearer. The only argument against my method I can think
>of is that of standardisation - bool isn't a standard C type and shouldn't
>be as it is still basically an int. However of all the additional types
>I have seen in people's source, bool is the one I've seen most often.

Well, depending on how you look at it, *everything* is an int (of one
length or another).  That's no reason to eliminate a data type.  Data
abstraction is one of the reasons we use high level languages in the
first place.  typedef bool makes perfect sense and is in keeping with
the C tradition of never spelling any word out if it can be avoided (if
u cn rd ths, u cn wrt pgms n C).  And for all you fortran fans out
there, how bout

  #define LOGICAL int

(yes, that's there in my friend's .h file, as well as INTEGER, REAL,
etc.).

I DEFINE all my case labels, magic numbers, etc., and more often than
not my variable names have more than 12 characters instead of less.  At
least I can look at code I wrote a couple years ago with a good chance
that I'll have some clue as to what it was I thought I was doing.



More information about the Comp.lang.c mailing list