|
|
|
|
|
|
|
(table continued from previous page) |
|
|
|
|
| Type | Size | Values | | long int | 4 bytes | -2,147,483,648 to 2,147,483,647 | | char | 1 byte | 256 character values | | bool | 1 byte | true or false | | float | 4 bytes | 1.2e38 to 3.4e38 | | double | 8 bytes | 2.2e308 to 1.8e308 |
|
|
|
|
|
|
You create, or define, a variable by stating its type, followed by one or more spaces, followed by the variable name and a semicolon. The variable name can be virtually any combination of letters, but cannot contain spaces. Legal variable names include x, J23qrsnf, and myAge. Good variable names tell you what the variables are for; using good names makes it easier to understand the flow of your program. The following statement defines an integer variable called myAge: |
|
|
|
|
|
|
|
|
As a general programming practice, try to use names that tell you what the variable is for. Names such as myAge or howMany are much easier to understand and remember than names like xJ4 or theInt. If you use good variable names, you'll need fewer comments to make sense of your code. |
|
|
|
|
|
|
|
|
Try this experiment: Guess what these pieces of programs do, based on the first few lines of code: |
|
|
|
|
|
|
|
|
main()
{
unsigned short x;
unsigned short y;
unsigned int z;
z = x * y;
} |
|
|
|
|
|
|
|
|
main ()
{
unsigned short Width;
unsigned short Length;
unsigned short Area;
Area = Width * Length;
} |
|
|
|
|
|