[ Pobierz całość w formacie PDF ]
message corresponding to an error number - this is not standard in C.) Each of these is used following a percent sign to indicate the type of output conversion, and between those two characters, the following fields may be added. - left justification in its field (n) a number specifying minimum field width . to separate n from m (m) significant fractional digits for a float l to indicate a "long" These are all used in the examples which are included in the program presently displayed on your monitor, with the exception of the string notation which will be covered later in this tutorial. Compile and run this program to see what effect the various fields have on the output. You now have the ability to display any of the data fields in the previous programs and it would be to your advantage to go back and see if you can display any of the fields anyway you desire. 4.8 Logical Compares Load and view the file namedcompares.cfor many examples of compare statements in C. main( ) /* this file will illustrate logical compares */ { int x = 11,y = 11,z = 11; char a = 40,b = 40,c = 40; float r = 12.987,s = 12.987,t = 12.987; /* First group of compare statements */ if (x == y) z = -13; /* This will set z = -13 */ if (x > z) a = A ; /* This will set a = 65 */ if (!(x > z)) a = B ; /* This will change nothing */ if (b if (r != s) t = c/2; /* This will set t = 20 */ /* Second group of compare statements */ if (x = (r != s)) z = 1000; /* This will set x = some positive number and z = 1000 */ if (x = y) z = 222; /* This sets x = y, and z = 222 */ if (x != 0) z = 333; /* This sets z = 333 */ if (x) z = 444; /* This sets z = 444 */ /* Third group of compare statements */ 4-5 C Tutorial Assignment & Logical compares x = y = z = 77; if ((x == y) && (x == 77)) z = 33; /* This sets z = 33 */ if ((x >y) || (z > 12)) z = 22; /* This sets z = 22 */ if ((x && y && z) z = 11; /* This sets z = 11 */ if ((x = 1) && (y = 2) && (z = 3)) r = 12.00; /* This sets x = 1, y = 2, z = 3, r = 12 .00 */ if ((x == 2) && (y = 3) && (z = 4)) r = 14.56; /* This doesn t change anything */ /* Fourth group of compares */ if (x == x); z = 27.345; /* z always gets changed */ if (x != x) z = 27.345; /* Nothing gets changed */ if (x = 0) z = 27.345; /* This sets x = 0, z is unchanged */ } We begin by defining and initializing nine variables to use in the following compare statements. This initialization is new to you and can be used to initialize variables while they are defined. The first group of compare statements represents the simplest kinds of compares since they simply compare two variables. Either variable could be replaced with a constant and still be valid compare, but two variables is the general case. The first compare checks to see if "x" is equal to "y" and it uses the double equal sign for the comparison. A single equal sign could be used here but it would have a different meaning as we will see shortly. The second comparison checks to see if "x" is greater than "z". The third introduces the "NOT" operator, the exclamation, which can be used to invert the result of any logical compare. The fourth checks for "b" less than or equal to "c", and the last checks for "r" not equal to "s". As we learned in the last chapter, if the result of the compare is true, the statement following the "if" clause will be executed and the results are given in the comments. Note that "less than" and "greater than or equal to" are also available, but are not illustrated here. It would be well to mention the different format used for the "if" statement in this example program. A carriage return is not required as a statement separator and by putting the conditional readability of the overall program. 4.9 More Compares The compares in the second group are a bit more involved. Starting with the first compare, we find a rather strange looking set of conditions in the parentheses. To understand this we must understand just what a "true" or "false" is in the C language. A "false" is defined as a value of zero, and "true" is defined as a non-zero value. Any integer or char type of variable can be used for the result of a true/false test, or the result can be an implied integer or char. [ Pobierz całość w formacie PDF ] |