Read Sams Teach Yourself C in 24 Hours Online
Authors: Tony. Zhang
printf(“0xFFFF ^ 0x8888 yields: 0x%X\n”,
33 067231861x AppxB 4.10.2000 11:05 AM Page 455
Answers to Quiz Questions and Exercises
455
0xFFFF ^ 0x8888);
printf(“0xABCD & 0x4567 yields: 0x%X\n”,
0xABCD & 0x4567);
printf(“0xDCBA | 0x1234 yields: 0x%X\n”,
0xDCBA | 0x1234);
return 0;
}
The output of the program is:
OUTPUT
0xFFFF ^ 0x8888 yields: 0x7777
0xABCD & 0x4567 yields: 0x145
0xDCBA | 0x1234 yields: 0xDEBE
5. The following is one possible solution:
/* 08A05.c */
#include
main()
{
int x;
printf(“Enter a character:\n(enter q to exit)\n”);
for ( x=’ ‘; x != ‘q’ ? 1 : 0; ) {
B
x = getc(stdin);
putchar(x);
}
printf(“\nOut of the for loop. Bye!\n”);
return 0;
}
Hour 9, “Working with Data Modifiers and
Math Functions”
Quiz
1. No. x contains a negative number that is not the same as the number contained by the unsigned int variable y.
2. You can use the long modifier, which increases the range of values that the int can hold. Or, if you are storing a positive number, you can also use the unsigned modifier to store a larger value.
3. %lu.
4. The header file is math.h.
33 067231861x AppxB 4.10.2000 11:05 AM Page 456
456
Appendix B
Exercises
1. The following is one possible solution:
/* 09A01 */
#include
main()
{
int x;
unsigned int y;
x = 0xAB78;
y = 0xAB78;
printf(“The decimal value of x is %d.\n”, x);
printf(“The decimal value of y is %u.\n”, y);
return 0;
}
The output of the program from my machine is:
OUTPUT
The decimal value of x is -21640.
The decimal value of y is 43896.
2. The following is one possible solution:
/* 09A02 */
#include
main()
{
printf(“The size of short int is %d.\n”,
sizeof(short int));
printf(“The size of long int is %d.\n”,
sizeof(long int));
printf(“The size of long double is %d.\n”,
sizeof(long double));
return 0;
}
3. The following is one possible solution:
/* 09A03 */
#include
main()
{
int x, y;
long int result;
x = 7000;
33 067231861x AppxB 4.10.2000 11:05 AM Page 457
Answers to Quiz Questions and Exercises
457
y = 12000;
result = x * y;
printf(“x * y == %lu.\n”, result);
return 0;
}
The output of the program from my machine is:
OUTPUT
x * y == 84000000.
4. The following is one possible solution:
/* 09A04 */
#include
main()
{
int x;
x = -23456;
printf(“The hex value of x is 0x%X.\n”, x);
return 0;
}
B
The output of the program from my machine is:
OUTPUT
The hex value of x is 0xA460.
5. The following is one possible solution:
/* 09A05.c */
#include
#include
main()
{
double x;
x = 30.0; /* 45 degrees */
x *= 3.141593 / 180.0; /* convert to radians */
printf(“The sine of 30 is: %f.\n”, sin(x));
printf(“The tangent of 30 is: %f.\n”, tan(x));
return 0;
}
6. The following is one possible solution (note that I’ve used the type casting (double) in the assignment statement x=(double)0x19A1;):
/* 09A06.c */
#include
#include
33 067231861x AppxB 4.10.2000 11:05 AM Page 458
458
Appendix B
main()
{
double x;
x = (double)0x19A1;
printf(“The square root of x is: %2.0f\n”, sqrt(x));
return 0;
}
Hour 10, “Controlling Program Flow”
Quiz
1. No.
2. The final result saved in x is 2, after the execution of three cases, ‘-’, ‘*’, and ‘/’.
3. The final result saved in x is 2. This time, only the case of operator = ‘-’ is executed due to the break statement.
4. The result saved by x is 27.
Exercises
1. The following is one possible solution:
/* 10A01.c Use the if statement */
#include
main()
{
int i;
printf(“Integers that can be divided by both 2 and 3\n”);
printf(“(within the range of 0 to 100):\n”);
for (i=0; i<=100; i++)
if (i%6 == 0)
printf(“ %d\n”, i);
return 0;
}
2. The following is one possible solution:
/* 10A02.c Use the if statement */
#include
33 067231861x AppxB 4.10.2000 11:05 AM Page 459
Answers to Quiz Questions and Exercises
459
main()
{
int i;
printf(“Integers that can be divided by both 2 and 3\n”);
printf(“(within the range of 0 to 100):\n”);
for (i=0; i<=100; i++)
if (i%2 == 0)
if (i%3 == 0)
printf(“ %d\n”, i);
return 0;
}
3. The following is one possible solution:
/* 10A03.c */
#include
main()
{
int letter;
printf(“Please enter a letter:\n”);
letter = getchar();
B
switch (letter){
case ‘A’:
printf(“The numeric value of A is: %d\n”, ‘A’);
break;
case ‘B’:
printf(“The numeric value of B is: %d\n”, ‘B’);
break;
case ‘C’:
printf(“The numeric value of C is: %d\n”, ‘C’);
break;
default:
break;
}
return 0;
}
4. The following is one possible solution:
/* 10A04.c */
#include
main()
{
int c;
33 067231861x AppxB 4.10.2000 11:05 AM Page 460
460
Appendix B
printf(“Enter a character:\n(enter q to exit)\n”);
while ((c = getc(stdin)) != ‘q’) {
/* no statements inside the while loop */
}
printf(“\nBye!\n”);
return 0;
}
5. The following is one possible solution:
/* 10A05.c */
#include
main()
{
int i, sum;
sum = 0;
for (i=1; i<8; i++){
if ((i%2 == 0) && (i%3 == 0))
continue;
sum += i;
}
printf(“The sum is: %d\n”, sum);
return 0;
}
Hour 11, “Understanding Pointers”
Quiz
1. By using the address-of operator, &. That is, the &ch expression gives the left value (the address) of the character variable ch.
2. The answers are as follows:
• Dereference operator
• Multiplication operator
• Multiplication operator
• The first and third asterisks are dereference operators; the second asterisk is a multiplication operator.
3. ptr_int yields the value of the address 0x1A38; *ptr_int yields the value of 10.
4. x now contains the value of 456.
33 067231861x AppxB 4.10.2000 11:05 AM Page 461
Answers to Quiz Questions and Exercises
461
Exercises
1. The following is one possible solution:
/* 11A01.c */
#include
main()
{
int x, y, z;
x = 512;
y = 1024;
z = 2048;
printf(“The left values of x, y, and z are:\n”);
printf(“0x%p, 0x%p, 0x%p\n”, &x, &y, &z);
printf(“The right values of x, y, and z are:\n”);
printf(“%d, %d, %d\n”, x, y, z);
return 0;
}
2. The following is one possible solution:
/* 11A02.c */
B
#include
main()
{
int *ptr_int;
char *ptr_ch;
ptr_int = 0; /* null pointer */
ptr_ch = 0; /* null pointer */
printf(“The left value of ptr_int is: 0x%p\n”,
ptr_int);
printf(“The right value of ptr_int is: %d\n”,
*ptr_int);
printf(“The left value of ptr_ch is: 0x%p\n”,
ptr_ch);
printf(“The right value of ptr_ch is: %d\n”,
*ptr_ch);
return 0;
}
3. The following is one possible solution:
/* 11A03.c */
#include
33 067231861x AppxB 4.10.2000 11:05 AM Page 462
462
Appendix B
main()
{
char ch;
char *ptr_ch;
ch = ‘A’;
printf(“The right value of ch is: %c\n”,
ch);
ptr_ch = &ch;
*ptr_ch = ‘B’; /* decimal 66 */
/* prove ch has been updated */
printf(“The left value of ch is: 0x%p\n”,
&ch);
printf(“The right value of ptr_ch is: 0x%p\n”,
ptr_ch);
printf(“The right value of ch is: %c\n”,
ch);
return 0;
}
4. The following is one possible solution:
/* 11A04.c */
#include
main()
{
int x, y;
int *ptr_x, *ptr_y;
x = 5;
y = 6;
ptr_x = &x;
ptr_y = &y;
*ptr_x *= *ptr_y;
printf(“The result is: %d\n”,
*ptr_x);
return 0;
}
Hour 12, “Understanding Arrays”
Quiz
1. It declares an int array called array_int with four elements. The statement also initializes the array with four integers, 12, 23, 9, and 56.
33 067231861x AppxB 4.10.2000 11:05 AM Page 463
Answers to Quiz Questions and Exercises
463
2. Because there are only three elements in the int array data, and the last element is data[2], the third statement is illegal. It may overwrite some valid data in the memory location of data[3].
3. The first array, array1, is a two-dimensional array, the second one, array2, is one-dimensional, the third one, array3, is three-dimensional, and the last one, array4, is a two-dimensional array.
4. In a multidimensional array declaration, only the size of the leftmost dimension can be omitted. Therefore, this declaration is wrong. The correct declaration looks like this:
char list_ch[][2] = {
‘A’, ‘a’,
‘B’, ‘b’,
‘C’, ‘c’,
‘D’, ‘d’,
‘E’, ‘e’};
Exercises
1. The following is one possible solution:
/* 12A01.c */
B
#include
main()
{
int i;
char array_ch[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘E’};
for (i=0; i<5; i++)
printf(“%c “, array_ch[i]);
return 0;
}
2. The following is one possible solution:
/* 12A02.c */
#include
main()
{
int i;
char array_ch[5];
for (i=0; i<5; i++)
array_ch[i] = ‘a’ + i;
for (i=0; i<5; i++)
printf(“%c “, array_ch[i]);
return 0;
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 464
464
Appendix B
3. The following is one possible solution:
/* 12A03.c */
#include
main()
{
int i, size;
char list_ch[][2] = {
‘1’, ‘a’,
‘2’, ‘b’,
‘3’, ‘c’,
‘4’, ‘d’,
‘5’, ‘e’,
‘6’, ‘f’};
/* method I */
size = &list_ch[5][1] - &list_ch[0][0] + 1;
size *= sizeof(char);
printf(“Method I: The total bytes are %d.\n”, size);
/* method II */
size = sizeof(list_ch);
printf(“Method II: The total bytes are %d.\n”, size);
for (i=0; i<6; i++)
printf(“%c %c\n”,
list_ch[i][0], list_ch[i][1]);
return 0;
}
4. The following is one possible solution:
/* 12A04.c */
#include
main()
{
char array_ch[11] = {‘I’, ‘ ‘,
‘l’, ‘i’, ‘k’, ‘e’, ‘ ‘,
‘C’, ‘!’, ‘\0’};
int i;
/* array_ch[i] in logical test */
for (i=0; array_ch[i]; i++)
printf(“%c”, array_ch[i]);
return 0;
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 465
Answers to Quiz Questions and Exercises
465
5. The following is one possible solution:
/* 12A05.c */
#include
main()
{
double list_data[6] = {
1.12345,
2.12345,
3.12345,
4.12345,
5.12345};
int size;
/* Method I */
size = sizeof(double) * 6;
printf(“Method I: The size is %d.\n”, size);
/* Method II */
size = sizeof(list_data);
printf(“Method II: The size is %d.\n”, size);
return 0;
}
B
Hour 13, “Manipulating Strings”
Quiz
1. The following two statements are legal:
• char str2[] = “A character string”;
• char str3 = “A”;
2. The following two statements are illegal:
• ptr_ch = ‘x’;
• *ptr_ch = “This is Quiz 2.”;
3. No. The puts() function appends a newline character to replace the null character at the end of a character array.
4. The %s format specifier is used for reading in a string; the %f is for a float number.
33 067231861x AppxB 4.10.2000 11:05 AM Page 466