1. Assume all variables are of type int. Find the value of each of the following variables:
a. x = (2 + 3) * 6;
b. x = (12 + 6)/2*3;
c. y = x = (2 + 3)/4;
d. y = 3 + 2*(x = 7/2);
a. x = 30 b. x = 27 c. y = 1, x = 1 d. x = 3, y = 9
2. Assume all variables are of type int. Find the value of each of the following variables:
a. x = (int) 3.8 + 3.3;
b. x = (2 + 3) * 10.5;
c. x = 3 / 5 * 22.0;
d. x = 22.0 * 3 / 5;
a. x = 6 b. x = 52 c. x = 0 d. x = 13
3. Evaluate each of the following expressions:
a. 30.0 / 4.0 * 5.0;
b. 30.0 / (4.0 * 5.0);
c. 30 / 4 * 5;
d. 30 * 5 / 4;
e. 30 / 4.0 * 5;
f. 30 / 4 * 5.0;
a. 37.5 b. 1.5 c. 35 d. 37 e. 37.5 f. 35.0
4. What will this program print?
#include <stdio.h>
#define FORMAT "%s! C is cool
"
int main(void) {
int num = 10;
printf(FORMAT, FORMAT);
printf("%d
", num);
printf("%d
", ++num);
printf("%d
", num++);
printf("%d
", num--);
printf("%d
", num);
return 0;
}
%s! C is cool ! C is cool 10 11 11 12 11
5. What will this program print?
#include <stdio.h>
#define TEN 10
int main(void) {
int n = 0;
while (n++ < TEN)
printf("%5d", n);
printf("
");
return 0;
}
1 2 3 4 5 6 7 8 9 10
6. Modify the last program so that it prints the letters a through g instead.
#include <stdio.h>
#define terminatingLetter 'g'
int main(void) {
char startingLetter = 'a';
while (startingLetter <= terminatingLetter)
printf("%5c", startingLetter++);
printf("
");
return 0;
}
7. If the following fragments were part of a complete program, what would they print?
a.
int x = 0;
while (++x<3)
printf("%4d", x);
b.
int x = 100;
while (x++ < 103)
printf("%4d
",x);
printf("%4d
",x);
c.
char c = 's';
while(ch<'w'){
printf("%c", ch);
ch++;
}
printf("%c
", ch);
a.
1 2
b.
101
102
103
104
c.
stuvw
8. What will the following program print?
#define MESG "COMPUTER BYTES DOG"
#include <stdio.h>
int main(void) {
int n = 0;
while (n < 5)
printf("%s
", MESG);
n++;
printf("That's all.
");
return 0;
}
This is an ill-constructed program. Because the while statement doesn't use braces, only the printf() statement is part of the loop, so the program prints the message COMPUTER BYTES DOG indefinitely until you can kill the program.