Sams Teach Yourself C in 24 Hours (66 page)

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

For instance, in the section “Creating a Linked List,” all functions that can be used to create a linked list and add or delete a node are put into the same module (24L01.c).

Data structure and variable declarations and function prototypes are saved into a header file (24L02.h). The main() function and the interface are saved into another module (24L03.c).

You can use a software engineering technique known as
information hiding
to reduce the complexity of programming. Simply speaking, information hiding requires a module to not provide information to other modules unless it’s very necessary.

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

420

Hour 24

The C compiler enables you to compile and debug different source files separately. In this way, you can focus on one source file at a time, and complete the compiling before you move to the next one. With the separate compilation, you can compile only those source files that have been changed and leave the source files that have already been compiled and debugged unattached.

If you’re interested in knowing more details about software engineering, you should study Ian Sommerville’s classic book,
Software Engineering
, which I’ve put into the list of recommended books at the end of this lesson.

Debugging

I’ve mentioned debugging several times in this lesson. What is a bug, anyway?

A
bug
in this context refers to any erroneous behavior of a computer system or a software program.
Debugging
means to find bugs and fix them. Please be aware that no computer system or software program is immune from bugs. Programmers, like you and me, make bugs because we’re human beings.

When you’re debugging your program, you should learn to isolate the erroneous behavior performed by your program. Many C compilers provide built-in debuggers that you can use to debug your program. Also, there are quite a few debugging tools made by third-party software vendors.

As it’s said, debugging requires patience, ingenuity, and experience. I recommend that you read a good book that will teach you all the techniques on debugging; in fact, I recommend one in the list of the books in the next section.

What You Have Learned

The following subsections provide you with a brief review on the basics of the C language. The review is a summary that you will find useful to brush up what you’ve learned in the previous hours.

C Keywords

In C, certain words have been reserved. These reserved words, called C
keywords
, have special meanings to the C language. The following are the C keywords:

auto

int

break

long

case

register

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

Where Do You Go From Here?

421

char

return

const

short

continue

signed

default

sizeof

do

static

double

struct

else

switch

enum

typedef

extern

union

float

unsigned

for

void

24

goto

volatile

if

while

Operators

Operators
can help you to manipulate data. C provides you with a rich set of operators.

Table 24.1 contains a list of the operators used in C.

TABLE 24.1

The Operators in C

Operator

Description

=

Assignment operator

+=

Addition assignment operator

-=

Subtraction assignment operator

*=

Multiplication assignment operator

/=

Division assignment operator

%=

Remainder assignment operator

-

Unary minus operator

++

Increment operator

--

Decrement operator

==

Equal to

!=

Not equal to

>

Greater than

continues

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

422

Hour 24

TABLE 24.1

continued

Operator

Description

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

sizeof

Size-of operator

&&

Logical AND operator

||

Logical OR operator

!

Logical NEGATION operator

&

Bitwise AND operator

|

Bitwise OR operator

^

Bitwise eXclusive OR (XOR) operator

~

Bitwise complement operator

>>

Right shift operator

<<

Left shift operator

?:

Conditional operator

Constants

Constants
are elements whose values do not change in the program. In C, there are several different types of constants.

Integer Constants

Integer constants
are decimal numbers. You can suffix an integer constant with u or U to specify that the constant is of the unsigned data type. An integer constant suffixed with l or L is a long int constant.

An integer constant is prefixed with a 0 (zero) to indicate that the constant is in the octal format. If an integer constant is prefixed with 0X or 0x, the constant is a hexadecimal number.

Character Constants

A character constant is a character enclosed by single quotes. For instance, ‘C’ is a character constant.

In C, there are several character constants that represent certain special characters (see Table 24.2).

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

Where Do You Go From Here?

423

TABLE 24.2

Special Characters in C

Character

Meaning

\a

Audible alert

\b

Backspace

\f

Form feed

\n

New line

\r

Carriage return

\t

Horizontal tab

\v

Vertical tab

\”

Double quote

\’

Single quote

24

\0

Null

\\

Backslash

\N

Octal constant (here N is an octal constant)

\xN

Hexadecimal constant (here N is a hexadecimal constant)

Floating-Point Constants

Floating-point constants
are decimal numbers that can be suffixed with f, F, l, or L to specify float or long double. A floating-point constant without a suffix is of the double data type by default. For instance, the following statements declare and initialize a float variable (flt_num) and a double variable (db_num):

float flt_num = 1234.56f;

double db_num = 1234.56;

A floating-point can also be represented in scientific notation.

String Constants

A
string constant
is a sequence of characters enclosed by double quotes. For instance,

“This is a string constant.” is a string constant. Note that the double quotes are not part of the content of the string. Also, the C compiler automatically adds a null character (\0) at the end of a string constant to indicate the end of the string.

Data Types

The basic
data types
provided by the C language are char, int, float, and double. In addition, there are array, enum, struct, and union data types that you can declare and use in your C programs.

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

424

Hour 24

The general form to define a list of variables with a specified data type is data_type

variable_name_list;

Here data_type can be one of the keywords of the data types. variable_name_list represents a list of variable names separated by commas.

The Array Data Type

An
array
is a collection of variables that are of the same data type. The following is the general form to declare an array:

data-type array-name[array-size];

Here data-type is the type specifier that indicates the data type of the array elements.

array-name is the name of the declared array. array-size defines how many elements the array can contain. Note that the brackets ([ and ]) are required in declaring an array.

The pair of [ and ] is also called the
array subscript operator
.

In addition, C supports multidimensional arrays.

The
enum
Data Type

enum is a short name for
enumerated
. The enumerated data type is used to declare named integer constants. The general form of the enum data type declaration is enum tag_name {enumeration_list} variable_list;

Here tag_name is the name of the enumeration. variable_list gives a list of variable names that are of the enum data type. Both tag_name and variable_list are optional.

enumeration_list contains defined enumerated names that are used to represent integer constants. Names represented by variable_list or enumeration_list are separated by commas.

The
struct
Data Type

In C, a structure collects different data items in such a way that they can be referenced as a single unit. The general form to declare a structure is

struct struct_tag {

data_type1 variable1;

data_type2 variable2;

data_type3 variable3;

.

.

.

};

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

Where Do You Go From Here?

425

Here struct is the keyword used in C to start a structure declaration. struct_tag is the tag name of the structure. variable1, variable2, and variable3 are the members of the structure. Their data types are specified respectively by data_type1, data_type2, and data_type3. The declarations of the members have to be enclosed within the opening and closing braces ({ and }) in the structure declaration, and a semicolon (;) has to be included at the end of the declaration.

The following is an example of a structure declaration:

struct automobile {

int year;

char model[8];

int engine_power;

float weight;

};

24

Here struct is used to start a structure declaration. automobile is the tag name of the structure. In the example here, there are three types of variables, char, int, and float.

The variables have their own names, such as year, model, engine_power, and weight.

They are all the members of the structure, and are declared with the braces ({ and }).

The
union
Data Type

A
union
is a block of memory that is used to hold data items of different types. In C, a union is similar to a structure, except that data items saved in the union are overlaid in order to share the same memory location. The syntax for declaring a union is similar to the syntax for a structure. The general form to declare a union is

union union_tag {

data_type1 variable1;

data_type2 variable2;

data_type3 variable3;

.

.

.

};

Here union is the keyword used in C to start a union declaration. union_tag is the tag name of the union. variable1, variable2, and variable3 are the members of the union.

Their data types are specified respectively by data_type1, data_type2, and data_type3.

The union declaration is ended with a semicolon (;).

The following is an example of a union declaration:

union automobile {

int year;

char model[8];

int engine_power;

float weight;

};

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

426

Hour 24

Here union specifies the union data type. automobile is the tag name of the union. The variables, such as year, model, engine_power, and weight, are the members of the union and are declared within the braces ({ and }).

Defining New Type Names with
typedef

You can create your own names for data types with the help of the typedef keyword, and use those names as synonyms for the data types. For instance, you can declare NUMBER as a synonym for the int data type:

typedef int NUMBER;

Then, you can start to use NUMBER to declare integer variables like this, NUMBER i, j;

which is equivalent to

int i, j;

Remember that a typedef definition must be made before the synonym made in the definition is used in any declarations in your program.

Expressions and Statements

An
expression
is a combination of constants or variables that is used to denote computations.

For instance,

(2 + 3) * 10

is an expression that adds 2 and 3 first, and then multiplies the result of the addition by 10.

In the C language, a
statement
is a complete instruction, ended with a semicolon. In many cases, you can turn an expression into a statement by simply adding a semicolon at the end of the expression.

A
null statement
is represented by an isolated semicolon.

A group of statements can form a
statement block
that starts with an opening brace ({) and ends with a closing brace (}). A statement block is treated as a single statement by the C compiler.

Control Flow Statements

In C, there is a set of control flow statements that can be divided into two categories: looping and conditional branching.

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

Where Do You Go From Here?

427

The
for
,
while
, and
do-while
Loops

The general form of the for statement is

for (expression1; expression2; expression3) {

statement1;

statement2;

.

.

.

Other books

A Crime of Fashion by Carina Axelsson
The Stonemason by Cormac McCarthy
The Carnival Trilogy by Wilson Harris
Forbidden Magic by Jennifer Lyon
Here to Stay by Suanne Laqueur
Doctor Who: Remembrance of the Daleks by Ben Aaronovitch, Nicholas Briggs, Terry Molloy
The Romero Strain by Alan, TS
Gods Go Begging by Vea, Alfredo
Almost Final Curtain by Hallaway, Tate
The Dom Project by Heloise Belleau, Solace Ames