Sams Teach Yourself C in 24 Hours (15 page)

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

52

Hour 3

Q&A

Q What is the difference between a constant and a variable?

A
The major difference is that the value of a constant cannot be changed, whereas the value of a variable can. You can assign different values to a variable whenever it’s necessary in your C program.

Q Why do you need a statement block?

A
Many C keywords can only control one statement. A statement block provides a way to put more than one statement together, and put the statement block under the control of a C keyword. Then, the statement block is treated as a single statement.

Q Which arithmetic operators have a higher precedence?

A
Among the five arithmetic operators, the multiplication, division, and remainder operators have a higher precedence than the addition and subtraction operators.

Q How many parts does a function normally have?

A
A function normally has six parts: the function type, the function name, the arguments, the opening brace, the function body, and the closing brace.

Workshop

To help solidify your understanding of this hour’s lesson, you are encouraged to answer the quiz questions and finish the exercises provided in the workshop before you move to the next lesson. The answers and hints to the questions and exercises are given in Appendix D, “Answers to Quiz Questions and Exercises.”

Quiz

1. In the C language, is 74 a constant? How about 571?

2. Is x = 570 + 1 an expression? How about x = 12 + y?

3. Are the following function names valid?

2methods

m2_algorithm

*start_function

Room_Size

.End_Exe

_turbo_add

4. Is 2 + 5 * 2 equal to (2 + 5) * 2?

5. Does 7 % 2 produce the same result as 4 % 3?

05 067231861x CH03 4.10.2000 10:59 AM Page 53

Learning the Structure of a C Program

53

Exercises

1. Given two statements, x = 3; and y = 5 + x;, how can you build a statement block with the two statements?

2. What is wrong with the following function?

int 3integer_add( int x, int y, int z)

{

int sum;

sum = x + y + z;

return sum;

}

3. What is wrong with the following function?

int integer_add( int x, int y, int z)

{

int sum;

3

sum = x + y + z

return sum;

}

4. Write a C function that can perform a multiplication of two integers and return the calculated result.

5. Write a C program that calls the C function you just wrote in Exercise 4 to calculate the multiplication of 3 times 5 and then print out the return value from the function on the screen.

05 067231861x CH03 4.10.2000 10:59 AM Page 54

06 067231861x CH04 4.10.2000 11:00 AM Page 55

HOUR 4

Understanding Data

Types and Keywords

What’s in a name? That which we call a rose

By any other name would smell sweet.

—W. Shakespeare

You learned how to make a valid name for a C function in Hour 3,

“Learning the Structure of a C Program.” Now, you’re going to learn more about naming a variable and the C keywords reserved by the C compiler in this hour.

Also in this hour you’re going to learn about the four data types of the C

language in detail:

• char data type

• int data type

• float data type

• double data type

06 067231861x CH04 4.10.2000 11:00 AM Page 56

56

Hour 4

C Keywords

The C language reserves certain words that have special meanings to the language. Those reserved words are sometimes called
C keywords
. You should not use the C keywords for your own variable, constant, or function names in your programs. Table 4.1 lists the 32

reserved C keywords.

TABLE 4.1

Reserved Keywords in C

Keyword

Description

auto

Storage class specifier

break

Statement

case

Statement

char

Type specifier

const

Storage class modifier

continue

Statement

default

Label

do

Statement

double

Type specifier

else

Statement

enum

Type specifier

extern

Storage class specifier

float

Type specifier

for

Statement

goto

Statement

if

Statement

int

Type specifier

long

Type specifier

register

Storage class specifier

return

Statement

short

Type specifier

signed

Type specifier

sizeof

Operator

static

Storage class specifier

struct

Type specifier

switch

Statement

06 067231861x CH04 4.10.2000 11:00 AM Page 57

Understanding Data Types and Keywords

57

Keyword

Description

typedef

Statement

union

Type specifier

unsigned

Type specifier

void

Type specifier

volatile

Storage class modifier

while

Statement

Don’t worry if you can’t remember all the C keywords the first time through. In the rest of the book, you’ll become more familiar with them and start to use many of the keywords through examples and exercises.

Note that all C keywords are written in lowercase letters. As I’ve mentioned, C is a case-sensitive language. Therefore, int, as shown in the list here, is considered as a C keyword, but INT is not.

The
char
Data Type

4

An object of the char data type represents a single character of the character set used by your computer. For example, A is a character, and so is a. But 7 is a number.

However, a computer can only store numeric code. Therefore, characters such as A, a, B, b, and so on all have a unique numeric code that is used by computers to represent the characters. Usually, a character takes 8 bits (that is, 1 byte) to store its numeric code.

For many computers, the ASCII codes are the
de facto
standard codes to represent a character set. (ASCII, just for your information, stands for American Standard Code for Information Interchange.) The original ASCII character set has only 128 characters because it uses the lower 7 bits that can represent 27 (that is, 128) characters.

On IBM-compatible PCs, however, the character set is extended to contain a total of 256

(that is, 28) characters.

One thing I’d like to mention here is that the ANSI C standard specifies only the value of the null character, which is always zero (i.e., a byte with all bits set to 0.) The other characters’ numeric values are determined by the types of computers, operating systems, and C compilers. You are encouraged to explore the character set of the computer you are using. This can be done with the program in Listing 4.1.

06 067231861x CH04 4.10.2000 11:00 AM Page 58

58

Hour 4

In the computer world, a bit is the smallest unit of data storage, and it can only have one of two values: 0 or 1. These values represent two states of electronic switches used in the computer’s memory and CPU. A byte is a

larger unit than a bit. In fact, eight bits are equal to one byte.

Character Variables

A variable that can represent different characters is called a
character variable
.

You can set the data type of a variable to char by using the following declaration format: char variablename;

where variablename is the name you provide in which to store values of this type.

If you have more than one variable to declare, you can either use the following format: char variablename1;

char variablename2;

char variablename3;

or this one:

char variablename1, variablename2, variablename3;

For example, the following statement declares MyCharacter and sets it to ‘A’: char MyCharacter = ‘A’;

Similarly, the following statements declare x, y, and z as character variables and then assign values to them:

char x, y, z;

x = ‘A’;

y = ‘f’;

z = ‘7’;

Note the last assignment, z = ‘7’, sets z to equal the numeric value representing the character ‘7’ in the character set — not the actual number 7.

You’ll learn more about the character variable and how to use it in your C programs later in this book.

Character Constants

A character enclosed in single quotes (‘) is called a
character constant
. For instance,

‘A’, ‘a’, ‘B’, and ‘b’, are all character constants that have their unique numeric values 06 067231861x CH04 4.10.2000 11:00 AM Page 59

Understanding Data Types and Keywords

59

in a given character set. For instance, you may see the unique numeric values from the ASCII character set.

It is important to remember that character constants are always surrounded by single quote characters (‘) while a string of more than one character uses the double quote (“).

If this sounds confusing, just remember that single quotes go with single characters. You saw an example of double quotes and character strings with the printf() function calls in the previous hour.

From the ASCII character set, you will find that the unique numeric (decimal) values of

‘A’, ‘a’, ‘B’, and ‘b’ are 65, 97, 66, and 98, respectively. Therefore, given x as a character variable, and given the ASCII character set, for instance, the following two assignment statements are equivalent:

x = ‘A’;

x = 65;

So are the following two statements:

x = ‘a’;

x = 97;

Later in this hour you’ll see a program, shown in Listing 4.2, that converts the numeric
4

values back to the corresponding characters.

Don’t confuse x = ‘a’; with x = a;. The first statement assigns the

numeric value of character a to variable x, that is, x will contain the value of 97 (the ASCII value of the letter ‘a’) after the assignment. Statement x = a; however assigns whatever value is contained in variable a into variable x.

You’ll learn more about the difference later in this book.

The Escape Character (
\
)

Actually, you first saw the escape character (\) in Hour 2, “Your First C Program,” when you learned to use the newline character (\n) to break a message into two pieces.

Therefore, the backslash (\) is called the
escape character
in the ASCII character set.

The escape character is used in the C language to tell the computer that a special character follows.

For instance, when the computer sees \ in the newline character \n, it knows that the next character, n, causes a sequence of a carriage return and a line feed.

06 067231861x CH04 4.10.2000 11:00 AM Page 60

60

Hour 4

Besides the newline character, some of the other special characters in the C language are as follows:

Character

Description

\b

The backspace character; moves the cursor to the left one character.

\f

The form-feed character; goes to the top of a new page.

\r

The return character; returns to the beginning of the current line.

\t

The tab character; advances to the next tab stop.

Printing Characters

You already know that the printf() function, defined in the C header file stdio.h, can be used to print out messages on the screen. (Ref. Listing 2.1 in Hour 2.) In this section, you’re going to learn to use the character format specifier, %c, which indicates to the printf() function that the argument to be printed is a character. (You’ll learn more about the format specifier in Hour 5, “Handling Standard Input and Output.” Here, you just get your feet wet.) The important thing to know for now is that each format specifier in the string you pass to printf() will correspond to one of the variables you pass in to the function. Let’s first have a look at the program in Listing 4.1, which prints out characters on the screen.

TYPE

LISTING 4.1

Printing Characters on the Screen

1: /* 04L01.c: Printing out characters */

2: #include

3:

4: main()

5: {

6: char c1;

7: char c2;

8:

9: c1 = ‘A’;

10: c2 = ‘a’;

11: printf(“Convert the value of c1 to character: %c.\n”, c1);

12: printf(“Convert the value of c2 to character: %c.\n”, c2);

13: return 0;

14: }

After the executable file of 04L01.c in Listing 4.1 is created, you can run it to see what will be printed out on the screen. On my machine, the executable file is named as 04L01.exe. The following is the output printed on the screen of my computer after I run the executable:

Other books

Edge of Night by Crystal Jordan
The Red House Mystery by A. A. Milne
Gracie by Suzanne Weyn
Wish Upon a Star by Klasky, Mindy
The Body In The Big Apple by Katherine Hall Page
The Other Traitor by Sharon Potts
9.0 - Sanctum by Bobby Adair
Secret of the Slaves by Alex Archer