C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
TABLE 5.1 Some of the Most Common Types of C Variables

Tip

In some older C compilers, int could hold only values between 32767 and -
32768. If you wanted to use a larger integer, you needed to use the long int type.
In most modern compilers, though, an int type can hold the same as a long int
type. If you’d like to be sure with your compiler, you can use the sizeof operator,
covered in Chapter 13, “A Bigger Bag of Tricks—Some More Operators for Your
Programs.”

Warning

You might notice that there are no string variables, although there are character string
literals. C is one of the few programming languages that has no string variables, but as
you’ll see in Chapter 6, “Adding Words to Your Programs,” you do have a way to
store strings in variables.

The Name column in Table 5.1 lists the keywords needed when you create variables for programs. In
other words, if you want an integer, you need to use the int keyword. Before completing your study
of variables and jumping into using them, you need to know one more thing: how to name them.


Naming Variables


All variables have names, and because you are responsible for naming them, you must learn the
naming rules. All variable names must be different; you can’t have two variables in the same program
with the same name.


A variable can have from 1 to 31 characters in its name. Some compilers do allow longer names, but
it’s better to stick with this limit, both for portability of code and to keep typing errors to a minimum.
(After all, the longer the name you use, the greater the chance for a typo!) Your program’s variables
must begin with a letter of the alphabet, but after that letter, variable names can have other letters,
numbers, or an underscore in any combination. All of the following are valid variable names:


Click here to view code image

Free download pdf