|
Page 3 of 3 Comparison Operators § C supports the comparison operators: < less than <= less than or equal to > greater than >= greater than or equal to == is equal to != is not equal to § These all give 1 (non zero value, i.e. true) when the comparison succeeds and 0 (i.e. false) when the comparison fails Comparison Operators C supports a full set of comparison operators. Each one gives one of two values to indicate success or failure. For instance in the following: int i = 10, j, k; j = i > 5; k = i <= 1000; The value 1, i.e. true, would be assigned to “j”. The value 0, i.e. false, would be assigned to “k”. Theoretically any arbitrary non zero integer value could be used to indicate success. 27 for instance is non zero and would therefore “do”. However C guarantees that 1 and only 1 will be used to indicate truth Logical Operators § C supports the logical operators: && and || or ! not § These also give 1 (non zero value, i.e. true) when the condition succeeds and 0 (i.e. false) when the condition fails int i, j = 10, k = 28; i = ((j > 5) && (k < 100)) || (k > 24); Logical Operators C supports the expected logical operators “and”, “or” and “not”. Unfortunately And, Or, Not although the use of the words themselves might have been more preferable, symbols “&&”, “||” and “!” are used instead. C makes the same guarantees about these operators as it does for the comparison operators, i.e. the result will only ever be 1 or 0. Logical Operator Guarantees C makes further guarantees about the logical operators. Not only will they C Guarantees produce 1 or 0, they are will be evaluated in a well defined order. The left-most condition is always evaluated first, even if the condition is more complicated, like: if(a && b && c && d || e) Here “a” will be evaluated first. If true, “b” will be evaluated. It true, “c” will be evaluated and so on. The next guarantee C makes is that as soon as it is decided whether a condition is true or false, no further evaluation is done. Thus if “b” turned out to be false, “c” and “d” would not be evaluated. The next thing evaluated would be “e”. Warning! An extra set of parentheses (round brackets) will always help to make code easier Parentheses to read and easier to understand. Remember that code is written once and maintained thereafter. It will take only a couple of seconds to add in extra parentheses, it may save several minutes (or perhaps even hours) of debugging Bitwise Operators As Brian Kernighan and Dennis Ritchie needed to manipulate hardware registers in their PDP-11, they needed the proper tools (i.e. bit manipulation operators) to do it. You will notice that the bitwise and, &, is related to the logical and, &&. As Brian & vs && and Dennis were doing more bitwise manipulation than logical condition testing, they reserved the single character for bitwise operation. Again the bitwise (inclusive) or, |, is related to the logical or, ||. | vs || A bitwise exclusive or is also provided. The ones compliment operator “~” flips all the bits in a value, so all 1s are turned to 0s, while all 0s are
|