08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2 ■ C – The Basics

2.3.4 Some Naming Conventions

Other than the rules for creating identifiers, C imposes no restriction on what names to use, or

what format (uppercase or lowercase, for instance) to use. However, good programming practice

dictates that some common-sense rules should be followed.

An identifier should be meaningful. For example, if it’s a variable, it should reflect the value

being stored in the variable; netPay is a much better variable than x for storing someone’s net

pay, even though both are valid. If it’s a function (Chapter 7), it should give some indication of

what the function is supposed to do; playGame is a better identifier than plg.

It is a good idea to use upper and lowercase combinations to indicate the kind of item named

by the identifier. In this book, we use the following conventions:

• A variable is normally written in lowercase: for example, sum. If we need

a variable consisting of two or more words, we start the second and

subsequent words with an uppercase letter: for example, voteCount or

sumOfSeries.

• A symbolic (or named) constant is an identifier that can be used in place of

a constant such as 100. Suppose 100 represents the maximum number of

items we wish to process in some program. We would probably need to use

the number 100 in various places in the program. But suppose we change

our mind and want to cater for 500 items. We would have to change all

occurrences of 100 to 500. However, we would have to make sure that we

do not change an occurrence of 100 used for some purpose other than the

maximum number of items (in a calculation like principal*rate/100).

• To make it easy to change our mind, we can set the identifier MaxItems to

100 and use MaxItems whenever we need to refer to the maximum number

of items. If we change our mind, we would only need to set MaxItems to the

new value. We will begin a symbolic constant with an uppercase letter. If it

consists of more than one word, we will begin each word with uppercase, as

in MaxThrowsPerTurn.

• We will see how to use symbolic constants in Section 4.6.

2.4 Basic Data Types

In Section 1.4 we briefly touched on the concept of a data type. For most of this book, we will use

the following data types:

int, double, and char

These, among others, are referred to as primitive data types.

Each data type defines constants of that type. When we declare a variable to be of a particular

type, we are really saying what kind of constants (values) can be stored in that variable. For

example, if we declare the variable num to be int, we are saying that the value of num at any time

can be an integer constant such as 25, -369, or 1024.

28

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!