{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Variables Arithmetic IO

# Variables Arithmetic IO - Variables Arithmetic IO Variables...

This preview shows pages 1–3. Sign up to view the full content.

Variables Arithmetic IO Variables and types It is very important when writing computer programs that one is able to save data in temporary locations, move data in and out of these locations and change the data over time. How we do this in C++ is with variables. Consider the mathematical formula (Pythagoras' formula). This formula can find the length of the hypotenuse of any right triangle. If we were to write a program that implemented this formula, then we would have a program that can find the length of the hypotenuse of any right triangle. This is clearly useful (though perhaps not as its own program). The reason the letters , , and are used in the formula is because they stand in for whatever values some particular right triangle has. Our program would ask for these values (specifically, the values for and ) from the user (or read them from a file, or whatever). Our program must also save these values in variables. However, unlike mathematics, C++ variables are typed . This means that every variable in C++ is a variable of a certain type. We have the following types available to us: int for integers (..., -2, -1, 0, 1, 2, ...) double for "floating point" values (i.e. decimal values like -0.5 or ) string for "strings" (i.e. text) bool for true/false values and some others... Variables in C++ are typed for two reasons: (1) programs can be executed more efficiently if the type is known beforehand and need not be determined based on what kinds of values the user provides (technically, this advantage is only gained because C++ is statically typed, which means the types of variables never change while the program is executing); (2) the C++ compiler is able to ensure that you are using variables the right way by checking if you are using values of the right type. For example, trying to save a string inside an integer type results in an error. Overflow and underflow Computers are finite machines, so they cannot store arbitrarily large values or arbitrarily small values (i.e. teeny-tiny fractions). Most programs do not need extremely large or small values, so

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
very rarely is the fact that computers are finite an issue. However, C++ types also have their limits, and some of these limits need to be considered when writing certain kinds of programs. For any general category of types such as integers, decimal numbers, etc. C++ has multiple specific types that you can choose from to use in your programs. For example, int is not the only C++ type that stores integer values. There is also char , short , long , long long , and possibly more depending on the compiler. As well, double is not the only type that can store decimal values. There is also float and long double . A quick comparison of these types will help you decide which is appropriate for each variable in your program. Integer types char can store values between -127 and 128 (signed) or between 0 and 255 (unsigned) short can store values between -32767 and 32767 (signed) or between 0 and 65535 (unsigned) int can store values between -2147483647 and 2147483647 (signed) or between 0 and 4294967295 long is the same as int (on CSE server) long long can store values between -9223372036854775807 and 9223372036854775807
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 11

Variables Arithmetic IO - Variables Arithmetic IO Variables...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online