Sams Teach Yourself C in 24 Hours (68 page)

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

The
C preprocessor
is not part of the C compiler. The C preprocessor runs before the compiler. During the preprocessing stage, all occurrences of a macro name are replaced by the macro body that is associated with the macro name. Note that a macro statement ends with a newline character, not a semicolon.

The C preprocessor also enables you to include additional source files to the program or compile sections of C code conditionally.

The #define directive tells the preprocessor to replace every occurrence of a macro name defined by the directive with a macro body that is associated with the macro name.

You can specify one or more arguments to a macro name defined by the #define directive.

The #undef directive is used to remove the definition of a macro name that has been previously defined.

The #ifdef directive controls whether a given group of statements is to be included as part of the program. The #ifndef directive is a mirror directive to the #ifdef directive; it enables you to define code that is to be included when a particular macro name is not defined.

The #if, #elif, and #else directives enable you to filter out portions of code to compile.

#endif is used to mark the end of an #ifdef, #ifndef, or #if block because the statements under the control of these preprocessor directives are not enclosed in braces.

The Road Ahead…

I believe that you can start to run now after you have learned to walk in the world of C

language programming through this book. Although you’re on your own, you’re not alone. You can revisit this book whenever you feel you need to. The following books, which I recommend to you, can also guide you in your continuous journey in the C

world:

The C Programming Language

by Brian Kernighan and Dennis Ritchie, published by Prentice Hall

C Interfaces and Implementations

by David Hanson, published by Addison-Wesley

30 067231861x CH24 1/25/00 10:48 AM Page 435

Where Do You Go From Here?

435

Practical C Programming

by Steve Oualline, published by O’Reilly & Associates, Inc.

No Bugs!—Delivering Error-Free Code in C and C++

by David Thielen, published by Addison-Wesley

Software Engineering

by Ian Sommerville, published by Addison-Wesley

The Mythical Man-Month: Essays on Software Engineering

by F. P. Brooks, Jr., published by Addison-Wesley

Dynamics of Software Development: “Don’t Flip the Bozo Bit” and 53 More Rules
for Delivering Great Software on Time

24

by Jim McCarthy, published by Microsoft Press

ISBN: 1-55615-823-8

Code Complete: A Practical Handbook of Software Construction

by Steve McConnell, published by Microsoft Press

ISBN: 1-55615-484-4

Summary

Before you close this book, I’d like to thank you for your patience and the effort you have put into learning the basics of the C language in the past 24 hours. (I think most of you may have spent more than 24 hours. It is quite normal. There are still a lot more things about C programming that you will learn as you go. But if you have already mastered the basic concepts, operators, and functions taught in this book, you can learn the new things quickly.) Now, it’s your turn to apply what you’ve learned from this book to solving the problems in the real world. Good luck!

30 067231861x CH24 1/25/00 10:48 AM Page 436

31 067231861x pt 6 1/25/00 11:06 AM Page 437

PART VI

Appendixes

Hour

A ANSI Standard Header Files

B Answers to Quiz Questions and Exercises

31 067231861x pt 6 1/25/00 11:06 AM Page 438

32 067231861x AppxA 4.10.2000 11:05 AM Page 439

APPENDIX A

ANSI Standard Header

Files

As you have learned in the past 24 hours, the C standard library comes with a set of include files called
header files
. These header files contain the declarations for the C library functions and macros, as well as relevant data types.

Whenever a C function is invoked, the header file(s) with which the C function is associated has to be included in your programs.

The following are the ANSI standard header files:

File

Description

assert.h

Contains diagnostic functions.

ctype.h

Contains character testing and mapping functions.

errno.h

Contains constants for error processing.

float.h

Contains constants for floating-point values.

limits.h

Contains implementation-dependent values.

continues

32 067231861x AppxA 4.10.2000 11:05 AM Page 440

440

Appendix A

File

Description

locale.h

Contains the setlocale() function, and is used to set locale

parameters.

math.h

Contains mathematics functions.

setjmp.h

Contains the setjmp() and longjmp() functions, and is used to

bypass the normal function call and return discipline.

signal.h

Contains signal-handling functions.

stdarg.h

Contains functions and macros for implementing functions that

accept a variable number of arguments.

stddef.h

Contains definitions for the ptrdiff_t, size_t, NULL, and errno

macros.

stdio.h

Contains input and output functions.

stdlib.h

Contains general utility functions.

string.h

Contains functions that are used to manipulate strings.

time.h

Contains functions for manipulating time.

If the C compiler on your machine is not 100 percent ANSI-conformable,

some of the ANSI header files might not be available with the compiler.

33 067231861x AppxB 4.10.2000 11:05 AM Page 441

APPENDIX B

Answers to Quiz

Questions and Exercises

Hour 1, “Taking the First Step”

Quiz

1. The lowest language mentioned in this hour that a computer can

understand directly is the machine language—that is, the binary code.

On the other hand, the highest language is the human language, such

as Chinese, English, French, and so on. Most high-level programming

languages, such as C, Java, and Perl, are close to the human language.

2. A computer cannot directly understand a program written in C. You

have to compile the program and translate it into binary code so that

the computer can read it.

3. Yes. That’s the beauty of the C language; you can write a program in C and save it into a library file. Later, you can invoke the program in another C program by including the library file.

33 067231861x AppxB 4.10.2000 11:05 AM Page 442

442

Appendix B

4. We need the ANSI standard for C to guarantee the portability of the programs written in C. Most C compiler vendors support the ANSI standard. If you write your program by following the rules set up by the ANSI standard, you can port your program to any machine by simply recompiling your program with a compiler that supports those machines.

Hour 2, “Writing Your First C Program”

Quiz

1. No. Actually, the C preprocessor will filter out all comments you put into your program before the compiler can see them. Comments are written for you or other programmers who look at your program.

2. An .obj file is created after a program is compiled by the C compiler. You still need a linker to link all .obj files and other library files together to make the final executable file.

3. No, the exit() function doesn’t return any values. However, the return statement does. In the main() function, if the return statement returns a value of 0, it indicates to the operating system that the program has terminated normally; otherwise, an error occurs.

4. A file that is required by the #include directive and ends with the extension .h is called a
header file
in C. Later in this book, you’ll learn that a header file contains the data or function declarations.

Exercises

1. No. The angle brackets (< and >) in the #include expression ask the C

preprocessor to look for a header file in a directory other than the current one. On the other hand, the #include “stdio.h” expression tells the C preprocessor to check the current directory first for the header file stdio.h, and then look for the header file in another directory.

2. The following is one possible solution:

/* 02A02.c */

#include

main()

{

printf (“It’s fun to write my own program in C.\n”);

return 0;

}

33 067231861x AppxB 4.10.2000 11:05 AM Page 443

Answers to Quiz Questions and Exercises

443

The output of the program is:

OUTPUT
It’s fun to write my own program in C.

3. The following is one possible solution:

/* 02A03.c */

#include

main()

{

printf (“Howdy, neighbor!\nThis is my first C program.\n”);

return 0;

}

The output of the program is:

OUTPUT
Howdy, neighbor!

This is my first C program.

4. The warning message I get when I try to compile the program is that the main() function should return a value of integer because, by default, the main() function returns an integer. Because the exit() function doesn’t return any values, you can replace exit() with the return statement.

5. I got two error (warning) messages on my machine. The first one is ‘printf’

B

undefined; the second one is ‘main’ : ‘void’ function returning a value.

To fix the first error, the header file, stdio.h, has to be included first before the printf() function can be called from the main() function; otherwise, you’ll get an error message during the linking stage. To fix the second error, you can remove the void keyword from the code.

Hour 3, “Learning the Structure of a C

Program”

Quiz

1. Yes. Both 74 and 571 are constants in C.

2. Yes. Both x = 571 + 1 and x = 12 + y are expressions.

3. 2methods, *start_function, and .End_Exe are not valid function names.

4. No. 2 + 5 * 2 is equivalent to 2 + 10, which gives 12; (2 + 5) * 2 is equivalent to 7 * 2, which produces 14.

5. Yes. Both 7 % 2 and 4 % 3 produce 1.

33 067231861x AppxB 4.10.2000 11:05 AM Page 444

444

Appendix B

Exercises

1. The following is one possible solution:

{

x = 3;

y = 5 + x;

}

2. The function name, 3integer_add, is illegal in C.

3. The second statement inside the function needs a semicolon at the end of the statement.

4. The following are two possible solutions:

Other books

Wait Until Tomorrow by Pat MacEnulty
Texas Bloodshed by William W. Johnstone
What a Girl Wants by Kristin Billerbeck
Prodigal Son by Dean Koontz
Firestorm by Lisa T. Bergren
Miss Wrong and Mr Right by Bryndza, Robert
Secrets of the Heart by Jenny Lane
A Fine Passage by France Daigle
Tomorrow's Dream by Janette Oke, Davis Bunn