/* This program demonstrates some uses of boolean operations. -G.C. Rutledge September 2002 */ #include int main(void) { int big = 10, small = 5, result; char c; printf("\nThe program demonstrates some uses of boolean operations.\n"); printf("Enter RETURN to step through examples.\n\n"); printf("Start with big=%d, small=%d\n",big,small); /* examples of relational operators */ printf("First, the relational operators:\n"); result = big > small; printf("big > small returns a value of... "); getchar(); printf("%d\n", result); result = big < small; printf("big < small returns a value of... "); getchar(); printf("%d\n", result); result = big == small; printf("big == small returns a value of... "); getchar(); printf("%d\n", result); result = big != small; printf("big != small returns a value of... "); getchar(); printf("%d\n", result); /* examples of logical operators */ printf("Now, some logical operators:\n"); result = (big > small) && (big != small); printf("(big > small) && (big != small) returns a value of... "); getchar(); printf("%d\n", result); result = (big < small) || (big == small); printf("(big < small) || (big == small) returns a value of... "); getchar(); printf("%d\n", result); result = (big - small) || (big == small); printf("(big - small) || (big == small) returns a value of... "); getchar(); printf("%d\n", result); /* beware of this common error: don't confuse = with == */ printf("Beware of this common mistake:\n"); result = (big < small) || (big = small); printf("(big < small) || (big = small) returns a value of... "); getchar(); printf("%d\n", result); printf("The value of big is now... "); getchar(); printf("%d\n", big); /* now watch this: */ result = !big; printf("Now watch this: The value of !big is... "); getchar(); printf("%d\n", result); /* conditional operator */ printf("Lastly, the conditional operator: \n"); printf("First, reset big = 10.\n"); big = 10; result = big>small?big:small; printf("The value of big>small?big:small is... "); getchar(); printf("%d\n",result); return (0); }