You are on page 1of 24

3 Data Types and Variables

3.1 The C++ Character Set
Learning Objectives
3.2 Keywords
Upon successful completion of this chapter, students will be
3.3 Basic Data Types able to:
3.3.1 Integer Data Types
3.3.2 Floating-Point Data Types
3.3.3 Character Data types § Describe C++ character set and keywords
3.3.4 sizeof Operator § Demonstrate an understanding of basic data types,
identifiers and variables constants needed to construct
3.4 Identifiers
a C++ program
3.5 Variables § Discuss the limitations applied to identifiers
3.5.1 Declaring Variables § Illustrate the differences between integer and floating
3.5.2 Initializing Variables point
§ Discuss the assignment and initialization values to
3.6 Constants
variables
3.7 Other Data Types § Discuss other data types and sizeof operator
3.8 Sample Programs
3.8.1 Sample Program 1
3.8.2 Sample Program 2
3.8.3 Sample Program 3

3.9 Summary

3.10 Exercises

Fundamentals of Programming

A computer program accepts input from the keyboard. The keys entered from keyboard will
be encoded as values and store in computer memory. The values can be numeric or alphabetic.
A computer program use data types to differentiate the numerical values and alphabetical
values. The memoy locations where the values are stored are represented by complicated
memory addresses. To simplify the read operation from the memory locations and write
operation to the memory locations for the values, variables are used to represent the memory
address. In this chapter, we will explain the basics data types, identifiers and variables needed
to construct a C++ program.

3 3.1 The C++ Character Set

A character set is an encoding scheme in which each character is represented by a different
binary value for computer identification. C++ supports a number of character sets. The C++
character set includes the keyboard characters listed below.

§ The uppercase letter A though Z
§ The lowercase letters a through z
§ The digits 0 through 9
§ Special characters (such as {, }, [, ], !, #, , %, , &, |, <, >, ;, :and *)
§ The blank and white space

3.2 Keywords

Keywords in C++ are also called reserved words, it is a vocabulary of commands that
humans can understand and that can be converted into machine language. The keywords have
standard, predefined meanings. The keywords should not be use for anything other than their
predefined purposes in the C++ language. Therefore, do not use them for variable name or
names for programmer-defined functions.

Here is a list of standard keywords in C++.

C++ Keywords
asm do inline short typeid
auto double int signed typename
bool dynamic_cast log sizeof union
break else mutable static unsigned
case enum namespace static_cast using
catch explicit new struct virtual
char extern operator switch void
class false private template volatile
const float protected this wchar_t
const_cast for public throw while
continue friend register true
default goto reinterpret_cast try
delete if return typedef
Table 3-1 C++ keywords

In addition to the keywords listed below, identifiers containing a double underscore (__) are
reserved for use by C++ implementations and standard libraries and should not be used in

48

68. The memoy locations where the values are stored are represented by complicated memory addresses. Variables can hold different types of data. A computer program use data types to differentiate the numerical values and alphabetical values. 7. -25.3 Basic Data Types A computer program accepts input from the keyboard. 1. In general there are two categories of data types: 3 § numeric data types (Examples: -50. To simplify the read operation from the memory locations and write operation to the memory locations for the values. and 65536) § and character data types C++ provides seven built-in data types and identifies them by keywords.6. These are also called primitive types because they are simple and uncomplicated. The values can be numeric or alphabetic. The keys entered from keyboard will be encoded as values and store in computer memory. Data Types and Variables 3. § short § int § long § unsigned § float § double § char 49 .0123. variables are used to represent the memory address. 0. -0.6.

056 point -0.648 2.483. Basic Data C++ Bytes Minimum Maximum Examples Type Keywords Value Value short short 2 -32.0 1.767 -99 integer (may be 1.147. A number of bytes are required for each data type to store value in memory. -24 depends on 048 3 compiler 101 implementation) 127 integer int 4 -2.0568 floating -0.01 41.0173 point 0. and their range is shown in Table 3-2 below.648 2.4 * 1038 -0.234 100.147.483. 256 depends on 1200 compiler 14400 implementation) 60000 floating float 4 -3.234 100.768 32. Fundamentals of Programming Every data type has a range of values.147.647 -9999 (may be 2.7 * 10308 1.4 * 1038 3. These are the highest and lowest values that you can store in memory which limited by the size of a data types.017 0.147.7 * 10308 -0.647 -222 integer 0 3333 65536 9999999 unsigned unsigned 2 or 4 0 4294967295 1 integer (may be 2.483.483.5678 character char 1 0 255 '\$' '*' '' 'a' 'A ' Table 3-2 Basic Data Types 50 . -1 depends on 0 compiler 566 implementation) 32767 long long 4 -2.567 double double 8 -1.

cout<<"quarter (float) = "<<quarter<<endl. cout<<"medium number (int) = "<<medium_number<<endl.3.25. float quarter = 0. 3. These will be discussed in later chapters. cout<<"positive number (unsigned) = "<<positive_number<<endl. int medium_number = 25000. You use unsigned if the values are all positive. } Program 3-1 Program that demonstrating data types Here is the output.14159 letter (char) = W The seven built-in data types also serve as the building blocks for more complex data types. called reference types or programmer-defined data types. small number (short) = 25 medium number (int) = 25000 big number (long) = 500000000 positive number (unsigned) = 50000 quarter (float) = 0. The types short. double pi = 3. You can use short if you known a variable will to hold only small values so you can save space in memory. // Output to screen cout<<"small number (short) = "<<small_number<<endl. unsigned and long are all variations of integer type. cout<<"letter (char) = "<<letter<<endl. 51 .1 Integer Data Types Integers are referring to whole numbers with no fraction part. long big_number = 500000000. You use long if you know you will be working with very large values. cout<<"pi (long) = "<<pi<<endl.h> main() { // Variable declarations and initializations short small_number = 25. Data Types and Variables Program 3-1 demonstrates the uses of data types in a program. 3 char letter = 'W'.14159. // Program that demonstrating data types #include <iostream.25 pi (long) = 3. cout<<"big number (long) = "<<big_number<<endl. unsigned positive_number = 50000. return 0.

1 = 32767 (positive integer. // Substract 1 from min_number min_number = min_number .h> main() { // Variable declarations and initializations short min_number = -32768. min_number = -32768 (negative integer) min_number . Fundamentals of Programming You can determine the actual number of btyes allocated by your computer for an integer value by using the sizeof operator introduced in section 3. and the largest (most positive) integer values that can be used in a program.147.483.647 Table 3-3 Integer values and word size 3 Program 3-2 shows the limit on the smallest (most negative). // Output to screen cout<<"\nmax_number = "<<max_number<<" (positive integer) "<<endl.3.1 = "<<min_number <<" (positive integer. // Program that demonstrating the largest and the smallest // integer values limits #include <iostream.1.768 32.4. cout<<"min_number + 1 = "<<max_number <<" (negative integer.483.147. cout<<"min_number . return 0. // Add 1 to max_number max_number = max_number + 1.767 4 bytes -2.648 2. Word Size Minimum Integer Value Maximum Integer Value 1 byte -128 127 2 bytes -32. over limit) max_number = 32767 (positive integer) min_number + 1 = -32768 (negative integer. } Program 3-2 Program that demonstrating the largest and the smallest values limits Here is the output. over limit) 52 . // Output to screen cout<<"min_number = "<<min_number<<" (negative integer) "<<endl. short max_number = 32767. over limit) "<<endl. over limit) "<<endl. The valid value supported by the memory allocation shown in Table 3-3 below.

A double data type can hold 14 or 15 significant digits of accuracy. } Program 3-3 Program that demonstrating significant digits of accuracy Here is the output.) or the exponent (e or E) or both.14286 double number (3.3. The number following the e represents a power of 10.27e-4 Table 3-4 Floating-point In exponential notation the letter e stands for exponent.142857142857.34 x 10-3 7.2 Floating-Point Data Types A floating-point is a decimal number that contains the decimal point (. a float given the value 0.142857142857) = "<<float_number<<endl. However. Program 3-3 below demonstrates the significant digits of accuracy for both float and double. For example. the significant digits of accuracy may be varied depend on compiler implementation. C++ has two floating point data types: float and double. float number (3. Data Types and Variables 3.54321e5 0. The term significant digits refer to the mathematical accuracy of a value.00734 7.34e-3 0.000627 6. // Program that demonstrating significant digits of accuracy #include <iostream. A float data type can hold values up to six or seven significants digits of accuracy.54321 x 105 6. // Output to screen cout<<"float number (3.14286 53 . cout<<"double number (3.h> main() { // Variable declarations and initializations float float_number = 3.324616777 will display as 0. double double_number = 3.142857142857.628 x 103 1.324617 because the value is only accurate to sixth decimal positions.27 x 10-4 6. return 0. look at the different ways of writing a decimal number as shown in Table 3-4 below: Decimal Notation Scientific Notation Exponential Notation 1628 1. For those who are not familiar with exponent.142857142857) = 3.142857142857) = "<<double_number<<endl.142857142857) = 3.628e3 3 654321 6.

#INF (Invalid Value) double number (1.7e38 or 2.4e+038 double number (3. cout<<"double number (2.7e308 or 1.7e38 or 2.4 * 10 ^38) = 3.#INF (Invalid Value) double number (3.5e+038 float number (1. // Output to screen cout<<"\nfloat number (3.h> main() { // Variable declarations and initializations float float_number = 3.4 * 10 ^38) = " <<float_number<<endl.7e308 or 1. double_number = 2.7e308.7 * 10 ^308) = 1.5 * 10 ^38) = " <<double_number<<endl.4e38 or 3. // Program that testing the precision and range of values for floating- number #include <iostream.4 * 10 ^38) = 3.5e38 or 3.7 * 10 ^308) = 1.7 * 10 ^308) = 1.#INF (Invalid Value) double number (2. cout<<"double number (3. double_number = 3.5e38. return 0.7e308.7e308 or 1.4e38 or 3. cout<<"double number (3.4e38.7 * 10 ^308) = "<<double_number <<" (Invalid Value)"<<endl.7 * 10 ^308) = 1.4 * 10 ^38) = " <<double_number<<endl.4e38 or 3.5e38 or 3. // Assigns new values float_number = 1.7e308. float number (3. // Assigns new values float_number = 2. } Here is the output.7 * 10 ^308) = "<<float_number <<" (Invalid Value)"<<endl. Fundamentals of Programming The following program tests the precision and range of values for floating-number.4e+038 float number (3. double_number = 1.7 * 10 ^308) = " <<double_number<<endl.5e38.7e308.4e38.4e38 or 3.5 * 10 ^38) = "<<float_number <<" (Invalid Value)"<<endl.7e38 or 2.5e38 or 3. double double_number = 3.7e38 or 2.5e38 or 3.#INF (Invalid Value) 54 . // Output to screen cout<<"\nfloat number (1.7 * 10 ^308) = "<<float_number <<" (Invalid Value)"<<endl. // Assigns new values float_number = 3.5 * 10 ^38) = 3.7e+308 float number (2. cout<<"double number (1.7e308 or 1. // Output to screen cout<<"\nfloat number (2.5 * 10 ^38) = 1. 3 // Output to screen cout<<"\nfloat number (3.

3 Character Data types You can use the char data type to hold any single character.3. 3 Character Data Types Examples Character '\$'.h> main() { // Character variable declaration and initialization char letter = 'A'. "Address" Table 3-5 Character data types Program 3-4 demonstrates the character and string. No:". When several characters are enclosed using two double quotation marks (" and "). cout<<"One line . 55 . '*'. so that the know the range of valid values can be assigned to a variable. // String variable declarations and initialization char one_line[60] = "The quick brown fox jumps over the lazy dog. Data Types and Variables 3. it is called a string. A character is any character enclosed using two single quotation marks (' and '). 3. 'a'."."<<one_line<<endl.The quick brown fox jumps over the lazy dog.3."<<letter<<endl.4 sizeof Operator C++ has a built-in operator called sizeof that gives the sizes of data in bytes. return 0. This is illustrated in Program 3-5 below. A letter . "Day of Birth". ' '. cout<<"A letter . "Tel. Programmers can use the sizeof operater to find out the sizes of a data type on a machine. Table 3-5 shows the examples of character data types. // Program that demonstrating character and string #include <iostream. } Program 3-4 Program that demonstrating character and string Here is the output.A One line . "Gender". '8' String "name". 'A'.

It is depends on compiler implementation. An identifier must start with either a letter or the underscore symbol. cout<<"Type long has a size of "<<sizeof(long)<<" bytes\n". meaning that the lowercase and uppercase letters in identifiers are treated as different characters. digits. Fundamentals of Programming // program to illustrate the size of data types in bytes #include <iostream. 3. 3 cout<<"Type double has a size of "<<sizeof(double)<<" bytes\n". however in practice. cout<<"Type unsigned has a size of "<<sizeof(unsigned)<<" bytes\n". 56 . and underscore (_). digits and underscores.4 Identifiers Identifiers are referring to memory locations which can hold values. or the underscore symbol. you are encourage to give them suggestive names that reflect the data items that they are going to store. Monthly_Rate. cout<<"Type char has a size of "<<sizeof(char)<<" bytes\n". C++ identifiers are case-sensitive. and all the rest of the characters must be letters. For example the identifier MONTH_RATE. They are formed by combining letters (both uppercase and lowercase). cout<<"Type int has a size of "<<sizeof(int)<<" bytes\n". Valid Identifier Invalid Identifier x "x" illegal character " num2 2num illegal first character month_rate month-rate illegal character - email email@ illegal character @ Student_Name STUDENT NAME illegal blank Table 3-6 Valid and invalid identifiers Identifiers can be of any length. return 0. They are referring to different memory locations.h> main() { cout<<"Type short has a size of "<<sizeof(short)<<" bytes\n". Although identifiers can be formed by freely combining letters. cout<<"Type float has a size of "<<sizeof(float)<<" bytes\n". they seldom exceed 25 characters. Monthly_rate and monthly_rate are all different. digits. } Program 3-5 Program to illustrate the size of data types in bytes The program outputs the following: Type short has a size of 2 bytes Type int has a size of 4 bytes Type unsigned has a size of 4 bytes Type long has a size of 4 bytes Type float has a size of 4 bytes Type double has a size of 8 bytes Type char has a size of 1 bytes The size used by the data types may be varied.

name = "Elizabeth". int b. The values of variables may change during the course of execution of a program. Table 3-7 Non-descriptive and descriptive identifiers 3. the mathematician can use the variable x in place of the constant 1. Table 3-7 shows the examples of non-descriptive and descriptive identifiers. That is. // Year int year. until the value of x is changed. Variable is most fundamental of all concepts in C++. As mentioned earlier. The concept of a variable is borrowed from mathematics. A statement such as x=1 stores the value 1 in the variable x. // Month int month.5 Variables Variables are identifiers. // Day int day. Unfortunately. From that point forward in the program. int c. C++ has a few more concerns about variables than the mathematician does. month.// Principal float principal Name. This session deals with the care and feeding of variables in C++. To simplify the read operation from the memory locations and write operation to the memory locations for the values. // Salary float salary.// Interest rate float interest_rate. You can make the assignment x = 1. Variables work the same way in C++. the value of x is 1. Here are more examples pi = 3. max_score = 100. identifiers also used for naming constants. letter = 'K'. float num. char letter.14159. From that point forward. 3 float value 2. year int a. structures and classes. the memory locations where the values are stored are represented by complicated memory addresses. gravity = 9. Descriptive identifiers make program easy to read. any references to x are the same as referencing 1. // Gender char gender. gender. // Name char name[40]. salary char oneline[40].81. Interest rate. Name Non Descriptive Identifier Descriptive Identifier Day. min_score = 0. functions. 57 . Data Types and Variables Besides being used for naming variables. variables are used to represent the memory address. principal float value1.

int x. service_tax. // Computes height * width. Fundamentals of Programming 3. they may be placed in the same statement. The syntax for variable declarations is as follows: 3 type_name variable_name. long number1.h> main() { // Variable declarations and initializations int height. cout<<"Enter the width: ". // Declares ch as character variable If there are more than one variable of the same type. Here are some examples of variable declarations. year. // Prompt for input cout<<"Enter the height: ". Declaring a variable tells the compiler to allocate appropriate memory space for the variable based on its data type.1 Declaring Variables Variables are used to store data of certain type. then assigns result to area area = height * width. code. width.5. float amount. Here are some examples of declaring a list of variables. char ch. All variables must be declared prior to their use. num2. area. type_name variable_list. // Declares number as a integer variable char ch. // Output result to screen cout<<"Area = "<<area. // Program that demonstrating variable declarations and variables manipulation #include <iostream. number2. y. You declare a variable by telling the compiler the name of the variable as well as the type of data it represents. This is called a variable declaration. unsigned day. // Declares amount as a floating-point variable int number. separated by commas. cin>>height. short num1. return 0. month. cin>>width. float interest_rate. } Program 3-6 Program that demonstrating variable declarations and manipulating variables 58 . Program 3-6 shows the variable declarations and manipulations. z.

char vehicle[6] = "lorry". x is initialized to 5 and y is initialized to 10 float rate. It is given the initial value "lorry".0 char ch = 'a'. This is the way to declare variables for storing strings. Both x and y are declared as integer variables. total = 0.0. the variable is said to be initialized. y = 10. Variable declaration Description and initialization in one step int x = 5.2 Initializing Variables Variables may be initialized by assigning values to them either at the time of their declaration or at later time. When a declaration statement is used to store a value in a variable. The square brackets [ ] are used to indicate the size of the character variable vehicle. Table 3-8 Variable declaration and initialization in one step 59 . Enter the height: 4 Enter the width: 8 Area = 32 3. Data Types and Variables Here is the result of sample run.5. 3 Table 3-8 shows some examples of variable declaration and initialization in one step. ch is declared as a character variable and is given initial value 'a'. Variable total is initialized to 0. The double quotation marks " and " are used to enclose the string "lorry". vehicle is declared as a character variable that cannot store more than six characters.

y = 10 total = 0. return 0. char ch. y. Variable declaration char vehicle[6]. area = 0. // Program that demonstrating variable declarations and initializations #include <iostream. cout<<"Area = "<<area<<endl. // Computes height x width. Variable initialization vehicle = "lorry". total. // Output result to screen cout<<"Height = "<<height<<endl. width = 8. then assigns result to area 3 area = height * width. ch ='a'. float rete. } Program 3-7 Program that demonstrating variable declarations and initializations in one step Here is the output. 60 . int x.h> main() { // Variable declarations and initializations int height = 4. Height = 4 Width = 8 Area = 32 Another way to initialize variables is to assign them values after the variables have been declared as shows below. Fundamentals of Programming Program 3-7 shows the variable declarations and initializations in one step.0. cout<<"Width = "<<width<<endl.

const float service_tax = 0. const char grade1 = 'A'. They can be type of integer. use the keyword const as in the following example const double pi = 3.h> main() { // Variable declarations int height. width. Height = 4 Width = 8 Area = 32 3. Data Types and Variables Here is the sample program. For example when pi is defined as const double pi = 3. return 0. // Output result to screen cout<<"Height = "<<height<<endl.. then assigns result to area area = height * width. width = 8. cout<<"Area = "<<area<<endl. cout<<"Width = "<<width<<endl.6 Constants Constants are values that do not change during program execution. const int days_of_year = 365. } Program 3-8 Program that demonstrating variable declarations and initializations Here is the output. character or floating point. // Program that demonstrating variable declarations and initializations #include <iostream. Mathematical constants are good candidates for receiving const status.1. 61 . it appropriately becomes a constant that should never take any value. area = 0. // Computes height x width.14159. To declare constants.14159. area. // Variable initializations 3 height = 4.

14159. area = PI * radius * radius.00 Circumference = 37. These will be discussed in the later chapter. cout<<"\nArea = "<<area. 62 . pointer. circum = TWO * PI * radius. and structures (will be discussed in later chapters).097 A constant must be declared and initialized before it can be used. double area. 3 cin>>radius.6991 Area = 113. Fundamentals of Programming Here is an example using constant. you can also define your own data types. You cannot change a constant’s value once it is declared. These include arrays.7 Other Data Types In addition to the data types discussed in this chapter.h> main() { const double PI = 3. return 0. radius. Besides these. cout<<"Circumference = "<<circum. circum. } Program 3-9 Program to illustrate constant Here is a sample run: Enter radius: 6. 3. C++ provides other data types derived from the basic data types. // To calculate the circumference and the area of a circle #include <iostream.0. called user-defined types. cout<<"\nEnter radius: ". const double TWO = 2.

} Program 3-10 Program computes the midpoint between the freezing and boiling points Here is the output. The midpoint between the freezing and boiling points of water is 50 degree celsius. // Display result cout<<"The midpoint between the freezing and boiling points of water is "<<mid_pt<<" degree celsius.1 Sample Program 1 Program 3-10 begins with a comment that explains what the program does. Water freezes at 0 and boils at 100 degrees celsius.h> main() { // Constant variable declaration const float freeze_pt = 0. // Freezing point of water const float boil_pt = 100. The body of the main function includes a declaration section where the constants freeze_pt and boil_pt. // Output to screen cout<<"Water freezes at "<<freeze_pt. and finaly display result on screen.0.". "<<endl.8 Sample Programs 3. computes the midpoint.0. return 0. // Boiling point of water // Variable declaration float mid_pt.8. cout<<" and boils at "<<boil_pt<<" degrees celsius. // Program computes the midpoint between the freezing and boiling points 3 // of water #include <iostream.0. These statement display a message. and variable mid_pt are defined. 63 . It is follow by a sequence of executable statements. Data Types and Variables 3. // Computes the midpoint mid_pt = (freeze_pt + boil_pt) / 2.

Finaly. cout<<"Service Tax (10%) = \$ "<<(amount * service_tax)<<endl. // Program that computes the amount after tax #include <iostream. It is follow by a sequence of input and output statements.05.h> main() { // Constant declaration const float government_tax = 0. // Computes the amount after tax amount_after_tax = amount + (amount * government_tax) + (amount * service_tax).1. // Display the result cout<<"You have to pay \$ "<<amount_after_tax. it display the result to screen.2 Sample Program 2 Program 3-11 begins with a comment that explains what the program does. The body of the main function includes a declaration section where the constants government_tax and service_tax. // Input and output statement cout<<"Enter the amount: ".0. Fundamentals of Programming 3. amount_after_tax = 0.0. return 0. and variable amount and amount_after_tax are defined. // Variable declaration float amount = 0. It is follow by an assignment statement that computes that 3 amount_after_tax. cout<<"Government Tax (5%) = \$ "<<(amount * government_tax)<<endl. These statements display a message request for an input for the variable amount. cin>>amount. Enter the amount: 100.8.00 Government Tax (5%) = \$ 5 Service Tax (10%) = \$ 10 You have to pay \$ 115 64 . service_tax = 0. } Program 3-11 Program that computes the amount after tax Here is the result of sample run. and display both the government tax and service tax required for the figure entered.

8. // Computes the age age = current_year . Data Types and Variables 3. cout<<"Enter current year (yyyy): ". cin>>name.h> main() { // Variable declaration char name[40] = "". Next year you will be "<<(age + 1) <<" years old. cin>>current_year. year = 0. month = 0. The body of the main function includes a declaration section where the variables name[40]. age = 0. month. Now you are 18 years old. // Output to screen cout<<"\nHi "<<name<<". computes the age.". // Prompt for inputs cout<<"Enter your name: ". return 0. unsigned short day = 0. year. Next year you will be 19years old.3 Sample Program 3 Program 3-12 begins with a comment that explains what the program does. cout<<"Enter your birthday (dd mm yyyy): ". Now you are "<<age <<" years old. It is follow by a sequence of executable statements. and finaly display result on screen. cin>>day>>month>>year. day. These statement prompt for inputs. 65 . 3 // Program that computes the age #include <iostream. unsigned short current_year = 0.year. } Program 3-12 Program that computes the age Here is the result of sample run. current_year and age are defined. Enter your name: Peter Enter your birthday (dd mm yyyy): 12 12 1989 Enter current year (yyyy): 2007 Hi Peter.

056 (approximately 6 -0.5678 character char 1 0 255 '\$' '*' '' 'a' 66 . long.536 1 (depends on 256 compiler 1200 implementation) 14400 60000 floating point float 4 -3. Therefore. namely.4 * 1038 3.0173 digits of precision) 0. the digits 0 through 9.7 * 10308 -0.4 * 1038 -0. and other special characters. Fundamentals of Programming 3.0568 (approximately 14 -0. double and char. Basic Data Type C++ Bytes Minimum Maximum Examples Keywords Value Value short integer short 1 (2) -128 127 -99 (depends on -24 compiler 048 implementation) 101 127 integer int 2 (4) -32. § The keywords should not be use for anything other than their predefined purposes in the C++ language.017 digits of precision) 0.483. unsigned.147.647 -222 (depends on 0 compiler 3333 implementation) 65536 9999999 unsigned integer unsigned 2 (4) 0 65.01 41. float. do not use them for variable name or names for 3 programmer-defined functions.234 100.648 2.7 * 10308 1.567 double floating point double 8 -1.0 1. the uppercase letter A through Z. § The C++ provides seven built-in data types: short.483. the lowercase letters a through z.234 100.767 -9999 (depends on -1 compiler 0 implementation) 566 32767 long integer long 4 (8) -2.147. int.768 32.9 Summary § The C++ character set includes the keyboard characters.

float interest_rate. short num1. Variable declaration Description and initialization in one step int x = 5.0. § Variables (identifiers) are named memory location that your program can use to store values. Identifiers must start with either a letter or the underscore symbol. float interest_rate. unsigned day. 67 . num2. code. ch is declared as a character variable and is given initial value 'a'. Valid Identifier Invalid Identifier 3 x "x" illegal character " num2 2num illegal first character month_rate month-rate illegal character - email email@ illegal character @ Student_Name STUDENT NAME illegal blank § All variables must be declared before they are used. digits. and the remains characters must be all be letter. § Variables may be initialized by assigning values to them either at the time of their declaration or at later time. service_tax. A variable name cannot be any reserved word. number2. double rate. x is initialized to 5 and y is initialized to 10 float rate. year. char code. char ch. Both x and y are declared as integer variables. total = 0. short num2. Variable Decalaration Variable Decalration in a List short num1. double price. unsigned month. char ch. z. y. or the underscore symbol. int y. double price. int x. rate. unsigned day. int z. Variable total is initialized to 0. float service_tax. unsigned year. y = 10. You can name a variable using any legal identifier. long number1. int x.0 char ch = 'a'. long number2. Data Types and Variables 'A' § C++ has a built-in operator called sizeof that gives the sizes of data in bytes. long number1. § Identifiers are used as names for variables and other items in a C++ program. month.

It is given the initial value "lorry". Fundamentals of Programming char vehicle[6] = "lorry". vehicle is declared as a character variable that cannot store more than six characters. § Constants are values that do not change during program execution. pointers and structures which are derived from the basic data types. 68 . § C++ provides other data types such as arrays. This is the way to declare variables for storing strings. The square brackets [ ] are used to indicate the size of the character variable vehicle. The double quotation marks " and " are used to enclose 3 the string "lorry".

year2k i. Double-precision constant: gravity = 9. memory location 5. last character c. When data cannot be changed after a program is compiled. @email 2. _file k. a. Integer constant: size = 100 l. Data Types and Variables 3. applications 4. tax. Floating-point variable: result1 = 0. For each of the following C++ programming language identifiers. Write appropriate declarations. net d.8 i.81 3.005. integer 3 d.10 Exercises 1. Character variable code = 'A' f. Long integer variable: bignum = 123456789 j. last e. Character variables: first. for each of the following a. compilers b. note whether they are valid or ilvalid. class_code h. result2 = -7. variables c. Double-precision variables: gross. weekly_sales b. data type d. abcdefghijklmnopq g. Character variable num = '9' g. main l. Short integer variable: month k. assigning initial values (if any). Unsigned integer variable: num c. Integer variable: index b. constant b. 2000car j. a. Programmer-named computer memory locations are called _____________. Character variable: eol = newline character h. variable c. Which of the following is not a basic data type in the C++ programming? 69 . the data is ____________. phone# f. address d. surname e. a.

70 . Which of the following values can you assign to a variable of type int? a. 10. an identifier c. Write a program compute and output the average of two floating-point number entered from the keyboard. 'W' d. an assigned value d. float 6.000 8. Write a program input a country’s name and capital’s name from the keyboard and output the same. int b. 9. Fundamentals of Programming a. 98. a type 3 b.000.000. Which of the following elements is not required in a variable declaration? a. Write a program compute and output the total of three integer numbers entered from keyboard. a semicolon 7. 0 b.000.8 c. char d. byte c. 9.