Sams Teach Yourself C in 24 Hours (6 page)

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

And try to make one function do one thing and do it very well.

Programming Examples

As mentioned earlier, this book contains many useful programming examples with explanations. These examples are meant to show you how to use different data types and functions provided in C.

01 067231861x intro 4.10.2000 10:58 AM Page 3

Introduction

3

Each example has a listing of the C program; the output generated from that listing will follow. The example also offers an analysis of how the program works. Special icons are used to point out each part of the example: Type, Input/Output, and Analysis.

In the example shown in Listing IN.1, there are some special typographic conventions.

The input you enter is shown in bold monospace type, and the output generated by the executable program of Listing IN.1 is shown in plain monospace type.

TYPE

LISTING IN.1

Read in a Character Entered by the User

1: /* INL01.c: Read input by calling getc() */

2: #include

3:

4: main()

5: {

6: int ch;

7:

8: printf(“Please type in one character:\n”);

9: ch = getc(stdin);

10: printf(“The character you just entered is: %c\n”, ch);

11: return 0;

12: }

The following output is displayed after the executable file, INL01.exe, is created and executed. The user enters the H character, and the program displays what the user entered.

Please type in one character:

H

The character you just entered is: H

In line 2 of Listing IN.1, the header file stdio.h is included for both the getc()
ANALYSIS
and printf() functions used in the program. Lines 4–12 give the name and body of the main() function.

In line 6, an integer variable ch is declared, which is assigned to the return value from the getc() function later in line 9. Line 8 prints out a piece of message that asks the user to enter one character from the keyboard. The printf() function in line 8 uses the default standard output stdout to display messages on the screen.

In line 9, the standard input stdin is passed to the getc() function, which indicates that the file stream is from the keyboard. After the user types in a character, the getc() function returns the numeric value (that is, an integer) of the character. Note that in line 9 the numeric value is assigned to the integer variable ch.

01 067231861x intro 4.10.2000 10:58 AM Page 4

4

Sams Teach Yourself C in 24 Hours

In line 10, the character entered is displayed on the screen with the help of printf().

Note that the character format specifier %c is used within the printf() function in line 10.

Q&A and Workshop

Each hour (that is, each chapter) ends with a Q&A section that contains answers to common questions relating to the lesson of the chapter. Following the Q&A section there is a Workshop that consists of quiz questions and programming exercises. The answers to these quiz questions and sample solutions for the exercises are presented in Appendix D,

“Answers to Quiz and Exercises.”

To help you solidify your understanding of each lesson, you are encouraged to try to answer the quiz questions and finish the exercises provided in the workshop.

Conventions Used in This Book

This book uses special typefaces to help you differentiate between C code and regular English, and to identify important concepts.

• Actual C code is typeset in a special monospace font. You’ll see this font used in listings, Input/Ouput examples, and code snippets. In the explanation of C features, commands, filenames, statements, variables, and any text you see on the screen are also typeset in this font.

• Command input and anything that you are supposed to enter appears in a bold monospace font. You’ll see this mainly in the Input/Output sections of examples.

• Placeholders in syntax descriptions appear in an italic monospace font. Replace the placeholder with the actual filename, parameter, or whatever element it represents.


Italics
highlight technical terms when they appear for the first time in the text and are sometimes used to emphasize important points.

What You’ll Learn in 24 Hours

Teach Yourself C in 24 Hours
consists of five parts. In Part I, “The Basics of C,” you’ll learn the basics of the C language. Here is a summary of what you’re going to learn:
Hour 1, “Taking the First Step,”
introduces you to the C language, the ANSI standard, and the basic software and hardware requirements for C programming.

Hour 2, “Your First C Program,”
demonstrates the entire procedure of writing, compiling, linking, and running a C program.

01 067231861x intro 4.10.2000 10:58 AM Page 5

Introduction

5

Hour 3, “Learning the Structure of a C Program,”
teaches you several important concepts, such as constants, variables, expressions, and statements. The anatomy of a function is introduced in this hour as well.

Hour 4, “Understanding Data Types and Keywords,”
lists all reserved C keywords.

Four data types, char, int, float, and double, are introduced in detail. Also, the rules for naming a variable are explained.

Hour 5, “Handling Standard Input and Output,”
teaches you to receive input from the keyboard, and print output on the screen with the help of a set of C functions, such as getc(), getchar(), putc(), putchar(), and printf().

Part II, “Operators and Control-flow Statements,” emphasizes operators and control-flow statements in C. The following is a summary of what you’ll learn:

Hour 6, “Manipulating Data,”
teaches you how to use arithmetic assignment operators, the unary minus operator, increment/decrement operators, relational operators, and the cast operator.

Hour 7, “Working with Loops,”
introduces looping (that is, iteration) with the for, while, or do-while statements.

Hour 8, “Using Conditional Operators,”
tells you about more operators, such as logical operators, bitwise operators, the sizeof operator, and ?: operator, which are frequently used in C.

Hour 9, “Working with Data Modifiers and Math Functions,”
describes how to use data modifiers to enable or disable the sign bit, or change the size of a data type.

Also, several mathematical functions provided by C are introduced.

Hour 10, “Controlling Program Flow,”
introduces all the control-flow statements used in C. They are the if, if-else, switch, break, continue, and goto statements.

Pointers and arrays are discussed in Part III, “Pointers and Arrays.” The following is a summary of what you’ll learn:

Hour 11, “Understanding Pointers,”
teaches you how to reference variables with pointers. Concepts such as left value and right value are also introduced.

Hour 12, “Understanding Arrays,”
explains how to declare and initialize arrays.

The relationship between the array and the pointer in C is discussed too.

Hour 13, “Manipulating Strings”
focuses on reading and writing strings. Several C

library functions, such as strlen(), strcpy(), gets(), puts(), and scanf(), are introduced to manipulate strings.

Hour 14, “Understanding Scope and Storage Classes,”
introduces block scope, function scope, program scope, and file scope. In addition, storage class specifiers or modifiers, such as auto, static, register, extern, const, and volatile are explained.

01 067231861x intro 4.10.2000 10:58 AM Page 6

6

Sams Teach Yourself C in 24 Hours

Part IV, “Functions and Dynamic Memory Allocation,” focuses on functions and dynamic memory allocations in C. The following is a summary of what you’ll learn:
Hour 15, “Working with Functions,”
describes the function declaration and definition in C. The function prototyping is explained, along with the function return type specification.

Hour 16, “Applying Pointers”
teaches you how to perform pointer arithmetic operations, access elements in arrays with pointers, and how to pass pointers to functions.

Hour 17, “Allocating Memory”
explains the concept of allocating memory dynamically. C functions, such as malloc(), calloc(), realloc(), and free(), are introduced with regard to the dynamic memory allocation.

Hour 18, “Using Special Data Types and Functions,”
introduces the enum data type and the use of typedef. Function recursion and command-line arguments to the main() function are also taught in Hour 18.

Part V, “Structure, Union, File I/O, and More,” discusses structures, unions, and disk file I/O in C. The following is a summary of what you’ll learn:

Hour 19, “Understanding Structures,”
introduces the structure data type. You learn to access structure members, and pass structures to functions with the help of pointers. Nested and forward-referencing structures are also discussed in this hour.

Hour 20, “Understanding Unions,”
describes the union data type, and the difference between union and structure. The applications of unions are demonstrated in several examples.

Hour 21, “Reading and Writing with Files,”
explains the concepts of the file and the stream in C. The basics of disk file input and output are introduced in this first part. The following C functions, along with several examples are, introduced in this hour: fopen(), fclose(), fgetc(), fputc(), fgets(), fputs(), fread(), fwrite(), and feof().

Hour 22, “Using Special File Functions,”
is the second part of disk file I/O, in which fseek(), ftell(), and rewind() are introduced to show how they can help you to get random access to disk files. In addition, the fscanf(), fprintf(), and freopen() functions are taught and invoked in sample programs.

Hour 23, “Compiling Programs: The C Preprocessor,”
describes the role played by the C preprocessor. You can learn the preprocessor directives, such as #define,

#undef, #ifdef, #endif, #ifndef, #if, #elis, and #else through the examples given in this hour.

01 067231861x intro 4.10.2000 10:58 AM Page 7

Introduction

7

Hour 24, “Where Do You Go from Here?,”
summarizes the important concepts and features introduced in this book. In addition, programming style, modular programming, and debugging are explained briefly. A list of recommended C books is provided for your further reading.

Now, you’re ready to start the journey of learning the C language, as the world has moved into a new millennium. Have a fun in reading this book, and enjoy programming in C!

Tony Zhang

Downingtown, Pennsylvania

January, 2000

01 067231861x intro 4.10.2000 10:58 AM Page 8

02 067231861x pt 1 1/25/00 10:43 AM Page 9

PART I

The Basics of C

Hour

1 Taking the First Step

2 Your First C Program

3 Learning the Structure of a C program

4 Understanding Data Types and Keywords

5 Handling Standard Input and Output

02 067231861x pt 1 1/25/00 10:43 AM Page 10

03 067231861x CH01 4.10.2000 10:59 AM Page 11

HOUR 1

Taking the First Step

A journey of a thousand miles is started by taking the first step.

—Chinese Proverb

High thoughts must have high language.

—Aristophanes

Welcome to
Teach Yourself C in 24 Hours
. In this first lesson you’ll learn the following:

• What C is

• Why you need to learn C

• The ANSI standard

• Hardware and software required to write and run C programs

03 067231861x CH01 4.10.2000 10:59 AM Page 12

12

Hour 1

What Is C?

C is a programming language. The C language was first developed in 1972 by Dennis Ritchie at AT&T Bell Labs. Ritchie called his newly developed language
C
simply because there was a
B
programming language already. (As a matter of fact, the B language led to the development of C.)

Other books

Simply Wicked by Kate Pearce
Campbell's Kingdom by Hammond Innes
The Blood Bargain by Reeves, Macaela
It's Nobody's Fault by Harold Koplewicz
Desire's Sirocco by Charlotte Boyett-Compo
Prey of Desire by J. C. Gatlin
Wrath of Hades by Annie Rachel Cole