Variables Arithmetic IO

Variables Arithmetic IO - Variables Arithmetic IO Variables...

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern