Session_2 - Lesson 3 Variables and Constants In C a...

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

View Full Document Right Arrow Icon
Lesson 3 – Variables and Constants In C++ a variable is a place to store information. A variable is a location in the computer’s memory in which you can store a value and from which you can later retrieve that value. When you define a variable in C++, you must tell the compiler what kind of variable it is: integer, character, etc. This information tells the compiler how much room to set aside and what kind of value you want to store in your variable. Please refer to the separate document “NumberSystems.doc” to learn more about the various ways to represent numeric data. An integer is either two bytes or four, depending on where you’re running the program. A sample program to determine the size of integers is: #include <iostream> using namespace std; int main() { cout << "size of int: " << sizeof ( int ) << endl; return 0; } To determine other sizes: #include <iostream> using namespace std; int main() { cout << "size of int: " << sizeof ( int ) << endl; cout << "size of short int: " << sizeof ( short int ) << endl; cout << "size of char: " << sizeof ( char ) << endl; cout << "size of long: " << sizeof ( long ) << endl; return 0; } Signed and Unsigned Signed integers are always positive or negative. Because you have the same number of bytes for both signed and unsigned integers, the largest number you can store in an unsigned integer is twice as big as the largest positive number. An unsigned short integer can handle numbers from 0 to 65,535. A signed short can only represent numbers from –32,768 to 32,767. An unsigned integer is always considered positive. Page 1 of 20
Image of page 1

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

View Full Document Right Arrow Icon
Floating point variables have values that can be expressed as fractions. Character variables hold a single byte and are used for holding the 256 characters and symbols of the ASCII and extended ASCII character sets. As an example, the same number (255) stored in a signed and unsigned variable gives different results: #include <iostream> using namespace std; int main() { unsigned char x; signed char y; y = 255; x = 255; cout << "x = " << ( int )x << endl; cout << "y = " << ( int )y << endl; return 0; } In addition, characters represent both integer (small integer) and an actual character: #include <iostream> using namespace std; int main() { unsigned char c; c = 65; cout << "c as a char: "<< c << endl; cout << "c as an int: "<< ( int )c << endl; return 0; } Fundamental Variable Types Some fundamental variable types are bool , unsigned short int , short int , unsigned long int , long int , int , char , float , double (see Table 3.1 on page 47). Boolean variables contain true or false (keywords in C++). #include <iostream> using namespace std; int main() { bool x; x = true ; cout << "value of bool is: " << x << endl; return 0; } Something to think about… The above code doesn’t work in “C” because bool is not a keyword in C. Page 2 of 20
Image of page 2
Defining a Variable You create or define a variable by stating its type, following by one or more spaces, following by the variable name and a semicolon. The variable name can be virtually any combination of letters, but it cannot contain spaces. When you declare a variable, memory is allocated (set aside) for that variable. The value of the variable will be whatever happened to be in memory at that time. (Note: not always true).
Image of page 3

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

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