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

Info iconThis preview shows pages 1–3. 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
Background image of page 1

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

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

This note was uploaded on 12/07/2010 for the course CS 175 taught by Professor Mike during the Spring '06 term at Golden West College.

Page1 / 20

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

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online