Read Sams Teach Yourself C in 24 Hours Online
Authors: Tony. Zhang
DataRead(fptr);
}
33 067231861x AppxB 4.10.2000 11:05 AM Page 497
Answers to Quiz Questions and Exercises
497
/* function definition */
long PtrTell(FILE *fptr)
{
long reval;
reval = ftell(fptr);
printf(“The fptr is at %ld\n”, reval);
return reval;
}
/* function definition */
void DataRead(FILE *fptr)
{
char buff[MAX_LEN];
fgets(buff, MAX_LEN, fptr);
printf(“%s”, buff);
}
/* function definition */
int ErrorMsg(char *str)
{
printf(“Cannot open %s.\n”, str);
B
return FAIL;
}
3. On my machine, the data.bin binary file is 10 bytes. The following is one possible solution:
/* 22A03.c */
#include
enum {SUCCESS, FAIL};
void DataWrite(FILE *fout);
void DataRead(FILE *fin);
int ErrorMsg(char *str);
main(void)
{
FILE *fptr;
char filename[]= “data.bin”;
int reval = SUCCESS;
if ((fptr = fopen(filename, “wb+”)) == NULL){
reval = ErrorMsg(filename);
} else {
DataWrite(fptr);
rewind(fptr);
DataRead(fptr);
33 067231861x AppxB 4.10.2000 11:05 AM Page 498
498
Appendix B
fclose(fptr);
}
return reval;
}
/* function definition */
void DataWrite(FILE *fout)
{
double dnum;
int inum;
dnum = 123.45;
inum = 10000;
printf(“%5.2f\n”, dnum);
fwrite(&dnum, sizeof(double), 1, fout);
printf(“%d\n”, inum);
fwrite(&inum, sizeof(int), 1, fout);
}
/* function definition */
void DataRead(FILE *fin)
{
double x;
int y;
printf(“\nRead back from the binary file:\n”);
fread(&x, sizeof(double), (size_t)1, fin);
printf(“%5.2f\n”, x);
fread(&y, sizeof(int), (size_t)1, fin);
printf(“%d\n”, y);
}
/* function definition */
int ErrorMsg(char *str)
{
printf(“Cannot open %s.\n”, str);
return FAIL;
}
4. The following is one possible solution:
/* 22A04.c */
#include
enum {SUCCESS, FAIL,
MAX_NUM = 3,
STR_LEN = 23};
void DataRead(FILE *fin);
int ErrorMsg(char *str);
33 067231861x AppxB 4.10.2000 11:05 AM Page 499
Answers to Quiz Questions and Exercises
499
main(void)
{
FILE *fptr;
char filename[]= “strnum.mix”;
int reval = SUCCESS;
if ((fptr = freopen(filename, “r”, stdin)) == NULL){
reval = ErrorMsg(filename);
} else {
DataRead(fptr);
fclose(fptr);
}
return reval;
}
/* function definition */
void DataRead(FILE *fin)
{
int i;
int miles;
char cities[STR_LEN];
printf(“The data read:\n”);
B
for (i=0; i
scanf(“%s%d”, cities, &miles);
printf(“%-23s %d\n”, cities, miles);
}
}
/* function definition */
int ErrorMsg(char *str)
{
printf(“Cannot open %s.\n”, str);
return FAIL;
}
Hour 23, “Compiling Programs: The C
Preprocessor”
Quiz
1. The semicolon (;) should not be included at the end of the macro definition because a macro definition ends with a newline, not a semicolon.
2. The value of 82 is assigned to result due to the assignment expression result =
1 + 9 * 9.
33 067231861x AppxB 4.10.2000 11:05 AM Page 500
500
Appendix B
3. The message of Under #else. is printed out.
4. The message of Under #ifdef. is printed out.
Exercises
1. The following is one possible solution:
/* 23A01.c */
#include
/* main() function */
main()
{
#define human 100
#define animal 50
#define computer 51
#define SUN 0
#define MON 1
#define TUE 2
#define WED 3
#define THU 4
#define FRI 5
#define SAT 6
printf(“human: %d, animal: %d, computer: %d\n”,
human, animal, computer);
printf(“SUN: %d\n”, SUN);
printf(“MON: %d\n”, MON);
printf(“TUE: %d\n”, TUE);
printf(“WED: %d\n”, WED);
printf(“THU: %d\n”, THU);
printf(“FRI: %d\n”, FRI);
printf(“SAT: %d\n”, SAT);
return 0;
}
2. The following is one possible solution:
/* 23A02.c */
#include
#define MULTIPLY(val1, val2) ((val1) * (val2))
#define NO_ERROR 0
main(void)
{
int result;
result = MULTIPLY(2, 3);
33 067231861x AppxB 4.10.2000 11:05 AM Page 501
Answers to Quiz Questions and Exercises
501
printf(“MULTIPLY(2, 3) produces value of %d.\n”, result);
return NO_ERROR;
}
3. The following is one possible solution:
/* 23A03.c */
#include
#define UPPER_CASE 0
#define NO_ERROR 0
main(void)
{
#if UPPER_CASE
printf(“THIS LINE IS PRINTED OUT,\n”);
printf(“BECAUSE UPPER_CASE IS DEFINED.\n”);
#elif LOWER_CASE
printf(“This line is printed out,\n”);
printf(“because LOWER_CASE is defined.\n”);
#else
printf(“This line is printed out,\n”);
printf(“because neither UPPER_CASE nor LOWER_CASE is defined.\n”);
#endif
B
return NO_ERROR;
}
4. The following is one possible solution:
/* 23A04.c: */
#include
#define C_LANG ‘C’
#define B_LANG ‘B’
#define NO_ERROR 0
main(void)
{
#if C_LANG == ‘C’
#if B_LANG == ‘B’
#undef C_LANG
#define C_LANG “I know C language.\n”
#undef B_LANG
#define B_LANG “Also, I know BASIC.\n”
printf(“%s%s”, C_LANG, B_LANG);
#else
#undef C_LANG
#define C_LANG “I only know C language.\n”
printf(“%s”, C_LANG);
33 067231861x AppxB 4.10.2000 11:05 AM Page 502
502
Appendix B
#endif
#elif B_LANG == ‘B’
#undef B_LANG
#define B_LANG “I only know BASIC.\n”
printf(“%s”, B_LANG);
#else
printf(“I don’t know C or BASIC.\n”);
#endif
return NO_ERROR;
}
34 067231861x index 1/25/00 10:45 AM Page 503
INDEX
~ (bitwise complement
== (equal to operator),
Symbols
operator), 131
98
| (bitwise OR operator),
\ (escape character), 59
+= (addition assignment
131
%% format specifier, 79
operator), 93
^ (bitwise XOR opera-
> (greater than opera-
& (ampersand)
tor), 131
tor), 98
address-of operator,
{} (braces), 45, 48
>= (greater than or equal
177-179
if statement, 156
to operator), 98
bitwise AND operator,
if-else statement, 159
++ (increment operator),
131
[ ] (brackets), 190
96-98
<> (angle brackets), 32
*/ (closing comment
<< (left-shift operator),
-> (arrow operator),
mark), 29
133-135
unions, 335, 351
:? (conditional operator),
< (less than operator), 98
= (assignment operator),
135-136
<= (less than or equal to
92
-- (decrement operator),
operator), 98
* (asterisk)
96-98
&& (logical AND opera-
deference operator, 182
/= (division assignment
tor), 124-126
determining meaning,
operator), 93
! (logical NEGATION
182
. (dot operator), unions,
operator), 128-129
multiplication operator,
335-337, 351
|| (logical OR operator),
182
” (double quotes), 32-33,
126-127
pointers, 180
59
*= (multiplication assign-
ment operator), 93
34 067231861x index 1/25/00 10:45 AM Page 504
504
!= (not equal to operator)
!= (not equal to opera-
ftell() function,
built-in
tor), 98
374-378
main() functions,
\0 (null character), 198
sequential disk files,
306
/* (opening comment
374
naming, 308
mark), 29
accessing
replacing, 308
// (opening comment
array elements, indexes,
command-line, 305
mark), 30
190
receiving, 306-308
() (parentheses)
arrays, via pointers,
passing to functions,
if statement, 156
264-266
47-48, 305
placing around expres-
addition assignment
variable, processing,
sions, 419
operator (+=), 93
252-254
% (remainder operator),
address variables.
See
argv arguments, 306
43
pointers
arithmetic assignment
%= (remainder assign-
address-of operator (&),
operators, 92-95
ment operator), 93
177-179, 323
addition assignment
>> (right-shift operator),
addresses
(+=), 93
133-135
left value, 176
division assignment
; (semicolons), 28
memory, 343
(/=), 93
’ (single quotes), 59
algorithms, implement-
multiplication assign-
-= (subtraction assign-
ing, 305
ment (*=), 93
ment operator), 93
aligning output, 83-84
remainder assignment
- (subtraction operator),
allocating memory
(%=), 93
96
calloc() function,
subtraction assignment
- (unary minus operator),
286-288
(-=), 93
95
malloc() function,
arithmetic expressions,
19L02.exe executable,
280-283
#if directive, 406
318
American National
arithmetic operators,
Standards Institute.
See
43-44
ANSI
array data type, 424
angle brackets (<>), 32
array element references,
A
ANSI (American
191
National Standards
array subscript operator
access
Institute), 15
([ ]), 190
random
C standard, 15-16
arrays, 190
code example,
header files, 439
accessing via pointers,
375-378
applying static specifiers,
264-266
disk files, 374-377,
230-231
character, 190, 210-211
387
argc arguments, 306
displaying, 196-198
fseek() function,
argument lists, 47
initializing, 208-209
374-378
arguments
declaring, 190
argc, 306
argv, 306
34 067231861x index 1/25/00 10:45 AM Page 505
books
505
elements, 190
integers to structures,
binary operators, multi-
accessing, 190
315
plication (*), 182
initializing, 191-192
inter values to enum
binary streams, 356, 370
integers, 190
data types, 296, 300