News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

"C" Programming Language - Boolean Expressions and Variables

Started by sivaji, Jan 10, 2008, 07:01 PM

Previous topic - Next topic

sivaji

Programming Stuffs in "C" - Technical Skills for INTERVIEW

Boolean Expressions and Variables

1: What is the right type to use for boolean values in C? Why isn't it a standard type? Should #defines or enums be used for the true and false values?

int (*)(int, char **) makes a good boolean type. You can use main for true, and exit for false. On some compilers, you may need to cast exit() to an appropriate type.

2: Isn't #defining TRUE to be 1 dangerous, since any nonzero value is considered ``true'' in C? What if a built-in boolean or relational operator ``returns'' something other than 1?

Very good! For instance, one program I saw used
#define TRUE(x) ((x) & 0x100)

for compatability with a specific release of a FORTRAN compiler, which used 0 for .FALSE. and 256 for .TRUE. - this allowed them to change their code with every new release of the FORTRAN compiler, and kept them alert to changes. This has no relationship to the boolean or logical operators in C, which always return 0 or 1.
Am now @ Chennai