15.04.2018 Views

programming-for-dummies

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

150<br />

Storing Data in a Variable<br />

Table 2-1<br />

Data Type<br />

Byte<br />

Boolean<br />

Character<br />

Integer<br />

Single<br />

Long<br />

Double<br />

String<br />

Typical Storage Requirements <strong>for</strong> Different Data Types<br />

Storage Size<br />

1 byte<br />

2 bytes<br />

2 bytes<br />

4 bytes<br />

4 bytes<br />

8 bytes<br />

8 bytes<br />

10 bytes + (2 * string length)<br />

(If you had a string consisting of three letters, that string would take up 10<br />

bytes of storage space plus (2 multiplied by three letters or 10 + (2 * 3),<br />

which is 16 bytes.)<br />

So when declaring variables as specific data types, you want to choose the<br />

data type that can<br />

✦ Hold a range of valid values.<br />

✦ Uses the least amount of space as possible.<br />

Storing Data in a Variable<br />

After you declare a variable, store data in that variable as the following C<br />

program demonstrates:<br />

int age;<br />

age = 15;<br />

Line 1: Declares the age variable as an integer data type.<br />

Line 2: Stuffs the number 15 into the age variable.<br />

You can assign a fixed value (like 15) to a variable or any equation that creates<br />

a value, such as<br />

taxes_owed = salary * 0.30;<br />

If the value of the salary variable is 1000, this command multiplies 1000<br />

(the salary variable’s value) by 0.30, which is 300. The number 300 then<br />

gets stored in the taxes_owed variable.

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

Saved successfully!

Ooh no, something went wrong!