Conditions
A condition is an expression that evaluates to a boolean value (true or false). We use conditions with if statements and other program flow statements to cause the program to make a decision or choice about what it should do.
Boolean Operators
Boolean expressions are the result of expressions that use boolean operators. Boolean operators return a true or false value:
a == 1 /* Boolean equality operator */ a != 1 /* Not equal to */ a > 1 /* Greater than */ a < 1 /* Less than */ a >= 1 /* Greater or equal to */ a <= 1 /* Less than or equal to */ (a == 1) || (b == 1) /* Boolean OR */ (a > 32) && (a < 90) /* Boolean AND */ !((a > 32) && (a < 90)) /* Boolean NOT */
Please take note of the equality operator, the double equal (==). This is not the same as the assignment operator (=). The assignment operator modifies the variable on the left hand side of the (=) sign. The (==) is a comparison operator, which yields true if the values of both expressions are the same, and false if they are not the same. It is very easy to accidentally put a single (=) sign in a comparison statement. It will often compile okay that way, but it won't do what you expect and it may be difficult to figure out why.
True or False
C does not have a datatype for boolean values (you could make one if you wanted). False is represented by an integer 0. True is represented by any integer value other than 0. Boolean operators yield a 1 when returning a true value, but the conditional statements (if, while, for, etc) will accept any non-zero value as meaning true.
Strings and strcmp()
Remember, in C there is no such thing as a string. Strings are character arrays. As such, the boolean comparison operators do not work the way you would naturally expect them to. Take the following example:
#include <stdio.h>
main()
{
char a[] = "Hello";
char b[] = "Hello";
if(a == b)
printf("a and b are equal\n");
}
a and b contain identical strings. Are they equal? This program will not think so. Remember, putting the name of a character array in your program actually references the address of the array. The if statement above is actually comparing the addresses of the two arrays. Since they are in two different locations, their addresses are not equal, the expression evaluates as false, and the printf() statement is never executed.
When you want to compare the contents of a character array, C provides the strcmp() function in its standard library. strcmp() takes two arguments (pointers to strings), and returns an integer value that is less than, equal to, or greater than zero if the first string is less than, equal to, or greater than the second string, respectively. To correctly implement the above program, my if statement would read:
if(strcmp(a, b) == 0)
If I wanted to see if the string in a was less than the string in b (for example, for sorting purposes), I would use:
if(strcmp(a, b) < 0)
There are variations of the strcmp() function, such as strncmp(), which examines only the first characters of the strings, and strcasecmp(), which compares the strings in a case insensitive fashion (this is sometimes stricmp() with the libraries that come with some compilers).