Sams Teach Yourself C in 24 Hours (71 page)

BOOK: Sams Teach Yourself C in 24 Hours
8.27Mb size Format: txt, pdf, ePub

466

Appendix B

Exercises

1. The following is one possible solution:

/* 13A01.c: Copy a string to another */

#include

#include

main()

{

int i;

char str1[] = “This is Exercise 1.”;

char str2[20];

/* Method I */

strcpy(str2, str1);

/* confirm the copying */

printf(“from Method I: %s\n”, str2);

/* Method II */

for (i=0; str1[i]; i++)

str2[i] = str1[i];

str2[i] = ‘\0’;

/* confirm the copying */

printf(“from Method II: %s\n”, str2);

return 0;

}

2. The following is one possible solution:

/* 13A02.c: Measure a string */

#include

#include

main()

{

int i, str_length;

char str[] = “This is Exercise 2.”;

/* Method I */

str_length = 0;

for (i=0; str[i]; i++)

str_length++;

printf(“The string length is %d.\n”, str_length);

/* Method II */

printf(“The string length is %d.\n”,

strlen(str));

return 0;

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 467

Answers to Quiz Questions and Exercises

467

3. The following is one possible solution:

/* 13A03.c: Use gets() and puts() */

#include

main()

{

char str[80];

int i, delt;

printf(“Enter a string less than 80 characters:\n”);

gets( str );

delt = ‘a’ - ‘A’;

i = 0;

while (str[i]){

if ((str[i] >= ‘A’) && (str[i] <= ‘Z’))

str[i] += delt; /* convert to lowercase */

++i;

}

printf(“The entered string is (in lowercase):\n”);

puts( str );

return 0;

}

B

4. The following is one possible solution:

/* 13A04.c: Use scanf() */

#include

main()

{

int x, y, sum;

printf(“Enter two integers:\n”);

scanf(“%d%d”, &x, &y);

sum = x + y;

printf(“The sum is %d\n”, sum);

return 0;

}

Hour 14, “Understanding Scope and Storage

Classes”

Quiz

1. The int variable x and float variable y, declared outside the myFunction() function, are global variables. The int variables, i and j, and the float variable y, 33 067231861x AppxB 4.10.2000 11:05 AM Page 468

468

Appendix B

declared inside the function, are local variables. Also, the two int variables, x and y, declared within a block inside myFunction(), are local variables with scope limited to the block.

2. For two variables sharing the same name, the compiler can figure out which one to use by checking their scopes. The latest declared variable becomes visible by replacing the variable that has the same name but is declared in the outer block. If, however, two variables sharing the same name are declared in the same block, the compiler will issue an error message.

3. The int variable i declared outside the myFunction() function has the same static storage class as the int variable x. The float variable y has an extern storage class.

Inside the myFunction() function, the two integer variables, i and j, have the auto storage class. The float variable z has an extern storage class, and the long variable s has a register storage class. index is an integer variable with a static storage class. The content of the character array str cannot be changed due to the const specifier.

4. No, it’s not legal. You cannot change the content of an array specified by the const specifier.

Exercises

1. The answers are as follows:

• { int x; }

• { const char ch; }

• { static float y; }

• register int z;

• char *ptr_str = 0;

2. The following is one possible solution:

/* 14A02.c */

#include

int x = 1234; /* program scope */

float y = 1.234567f; /* program scope */

void function_1(int x, double y)

{

printf(“From function_1:\n x=%d, y=%f\n”, x, y);

}

main()

33 067231861x AppxB 4.10.2000 11:05 AM Page 469

Answers to Quiz Questions and Exercises

469

{

int x = 4321; /* block scope 1*/

function_1(x, y);

printf(“Within the main block:\n x=%d, y=%f\n”, x, y);

/* a nested block */

{

float y = 7.654321f; /* block scope 2 */

function_1(x, y);

printf(“Within the nested block:\n x=%d, y=%f\n”, x, y);

}

return 0;

}

3. The following is what I obtained from running the C program given in this exercise:

x=0, y=0

x=0, y=1

x=0, y=2

x=0, y=3

x=0, y=4

Because x has a temporary storage with the block scope, and y has a permanent storage, x is set to 0 every time the program execution enters the for loop, but the
B

value saved in y is kept.

4. The following is one possible solution:

/* 14A04.c: Use the static specifier */

#include

/* the add_two function */

int add_two(int x, int y)

{

static int counter = 1;

static int sum = 0;

printf(“This is the function call of %d,\n”, counter++);

printf(“the previous value of sum is %d,\n”, sum);

sum = x + y;

return sum;

}

/* the main function */

main()

{

int i, j;

for (i=0, j=5; i<5; i++, j--)

printf(“the addition of %d and %d is %d.\n\n”,

i, j, add_two(i, j));

return 0;

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 470

470

Appendix B

Hour 15, “Working with Functions”

Quiz

1. The answers are as follows:

• int function_1(int x, int y); is a function declaration with a fixed

number of arguments.

• void function_2(char *str); is a function declaration with a fixed number of arguments.

• char *asctime(const struct tm *timeptr); is a function declaration

with a fixed number of arguments.

• int function_3(void); is a function declaration with no arguments.

• void function_5(void); is a function declaration with no arguments.

• char function_4(char c, ...); is a function declaration with a variable number of arguments.

2. The second expression is a function definition; that is,

int function_2(int x, int y) {return x+y;}

3. The int data type is the default data type returned by a function if a type specifier is omitted.

4. The third one, char function_3(...);, is illegal.

Exercises

1. The following is one possible solution:

/* 15A01.c: */

#include

#include

void GetDateTime(void);

main()

{

printf(“Before the GetDateTime() function is called.\n”);

GetDateTime();

printf(“After the GetDateTime() function is called.\n”);

return 0;

}

/* GetDateTime() definition */

void GetDateTime(void)

{

time_t now;

int i;

33 067231861x AppxB 4.10.2000 11:05 AM Page 471

Answers to Quiz Questions and Exercises

471

char *str;

printf(“Within GetDateTime().\n”);

time(&now);

str = asctime(localtime(&now));

printf(“Current date and time is: “);

for (i=0; str[i]; i++)

printf(“%c”, str[i]);

}

2. The following is one possible solution:

/* 15A02.c */

#include

int MultiTwo(int x, int y);

main ()

{

printf(“The result returned by MultiTwo() is: %d\n”,

MultiTwo(32, 10));

return 0;

}

/* function definition */

B

int MultiTwo(int x, int y)

{

return x * y;

}

3. The following is one possible solution:

/* 15A03.c */

#include

#include

int MultiInt(int x, ...);

main ()

{

int d1 = 1;

int d2 = 2;

int d3 = 3;

int d4 = 4;

printf(“Given an argument: %d\n”, d1);

printf(“The result returned by MultiInt() is: %d\n\n”,

MultiInt(1, d1));

printf(“Given an argument: %d, %d, %d, and %d\n”, d1, d2, d3, d4);

printf(“The result returned by MultiInt() is: %d\n\n”,

MultiInt(4, d1, d2, d3, d4));

return 0;

33 067231861x AppxB 4.10.2000 11:05 AM Page 472

472

Appendix B

}

/* definition of MultiInt() */

int MultiInt(int x, ...)

{

va_list arglist;

int i;

int result = 1;

printf(“The number of arguments is: %d\n”, x);

va_start (arglist, x);

for (i=0; i

result *= va_arg(arglist, int);

va_end (arglist);

return result;

}

4. The va_arg() fetches arguments from left to right on my machine. The following is one possible solution:

/* 15A04.c */

#include

#include

double AddDouble(int x, ...);

main ()

{

double d1 = 1.5;

double d2 = 2.5;

double d3 = 3.5;

double d4 = 4.5;

printf(“Given an argument: %2.1f\n”, d1);

printf(“The result returned by AddDouble() is: %2.1f\n\n”,

AddDouble(1, d1));

printf(“Given arguments: %2.1f and %2.1f\n”, d1, d2);

printf(“The result returned by AddDouble() is: %2.1f\n\n”,

AddDouble(2, d1, d2));

printf(“Given arguments: %2.1f, %2.1f and %2.1f\n”, d1, d2, d3);

printf(“The result returned by AddDouble() is: %2.1f\n\n”,

AddDouble(3, d1, d2, d3));

printf(“Given arguments: %2.1f, %2.1f, %2.1f, and %2.1f\n”, d1, d2, d3, d4);

printf(“The result returned by AddDouble() is: %2.1f\n”,

AddDouble(4, d1, d2, d3, d4));

return 0;

}

/* definition of AddDouble() */

33 067231861x AppxB 4.10.2000 11:05 AM Page 473

Answers to Quiz Questions and Exercises

473

double AddDouble(int x, ...)

{

va_list arglist;

int i;

double argument, result = 0.0;

printf(“The number of arguments is: %d\n”, x);

va_start (arglist, x);

for (i=0; i

argument = va_arg(arglist, double);

printf(“Argument passed to this function: %f\n”, argument);

result += argument;

}

va_end (arglist);

return result;

}

Hour 16, “Applying Pointers”

Quiz

B

1. I obtain the following answers from my machine:

• 4 bytes

• 4 bytes

• 4 bytes

• 12 bytes

• 12 bytes

• 12 bytes

2. Because 0x100A - 0x1006 gives 4, and one int takes 2 bytes, ptr1 and ptr2 are two integers apart. Therefore, the answer is 2.

3. 0x0230 and 0x0260.

4. The answers are as follows:

• *(ptr + 3) fetches ‘A’.

• ptr - ch gives 1.

• *(ptr - 1) fetches ‘a’.

• *ptr = ‘F’ replaces ‘b’ with ‘F’.

33 067231861x AppxB 4.10.2000 11:05 AM Page 474

474

Appendix B

Exercises

1. The following is one possible solution:

/* 16A01.c */

#include

void StrPrint(char *str);

main()

{

char string[] = “I like C!”;

StrPrint(string);

return 0;

}

void StrPrint(char *str)

{

printf(“%s\n”, str);

}

2. The following is one possible solution:

/* 16A02.c */

#include

void StrPrint(char *str);

main()

{

char string[] = “I like C!”;

char *ptr;

int i;

ptr = string;

for (i=0; ptr[i]; i++){

if (ptr[i] == ‘i’)

ptr[i] = ‘o’;

if (ptr[i] == ‘k’)

ptr[i] = ‘v’;

}

StrPrint(ptr);

return 0;

}

void StrPrint(char *str)

{

printf(“%s\n”, str);

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 475

Answers to Quiz Questions and Exercises

475

3. The following is one possible solution:

/* 16A03.c */

#include

void StrPrint(char str[][15], int max);

main()

{

char str[2][15] = {

“You know what,”,

“C is powerful.” };

StrPrint(str, 2);

return 0;

}

void StrPrint(char str[][15], int max)

{

int i;

for (i=0; i

printf(“%s\n”, str[i]);

}

4. The following is one possible solution:

B

* 16A04.c */

#include

/* function declarations */

void StrPrint1(char **str1, int size);

void StrPrint2(char *str2);

/* main() function */

main()

{

char *str[7] = {

“Sunday”,

“Monday”,

“Tuesday”,

“Wednesday”,

“Thursday”,

“Friday”,

“Saturday”};

int i, size;

size = 7;

StrPrint1(str, size);

for (i=0; i

StrPrint2(str[i]);

33 067231861x AppxB 4.10.2000 11:05 AM Page 476

476

Appendix B

return 0;

}

/* function definition */

void StrPrint1(char **str1, int size)

Other books

Home for a Soldier by Tatiana March
Violet Path by Olivia Lodise
Surviving Michael by Birchall, Joseph
Got You Back by Jane Fallon
vN by Madeline Ashby
The Unscrupulous Uncle by Allison Lane
Inhuman by Kat Falls