1.6 KiB
1.6 KiB
- The following program fragments illustrate the logical operators. Show the output produced by each, assuming that
i,j, andkareintvariables.
a. i = 10; j = 5;
printf("%d", !i < j);
// Expected output: `1`, because `!i` evaluates to 0, and 0 is less than 5, so that expression evaluates to true, or 1.
b. i = 2; j = 1;
printf("%d", !!i + !j);
// Expected output: `1`, because !!2 evaluates to 1, and !j evaluates to 0
c. i = 5; j = 0; k = -5;
printf("%d", i && j || k);
// Expected output: `1`, because i && j should evaluate to 0, but `0 || 1` should evalulate to true.
d. i = 1; j = 2; k = 3;
printf("%d", i < j || k);
// Expected output: `1`
- Write a single expression whose value is either
-1,0, or1depending on whetheriis less than, equal to, or greater thanj, respectively.
/*
If i < j, the output should be -1.
If i == j, the output should be zero
If i > j, the output should be 1.
*/
(i > j) - (i < j)
- Is the following
ifstatement legal?
if (n == 1-10)
printf("n is between 1 and 10\n");
Yes the statement is legal, but it does not produce the intended effect. It would not produce an output when n = 5, because 1-10 evaluates to -9, and -9 != 5.
- What output does the following program fragment produce? (Assume that
iis an integer variable.)
int i = 1;
switch (i % 3) {
case 0: printf("zero");
case 1: printf("one");
case 2: printf("two");
}
The program would print onetwo because each case is missing a break statement.
- The following table shows the telephone ar