++ Identifiers and Datatypes

Each line of a C++ program contains a number of different words. The C++ compiler classifies these words into different categories:

Reserved words:

A reserved is a word that has a special meaning in C++. A reserved word always appears in lowercase and can only be used for the purpose, which has been defined to it by the C++ compiler.

Examples of reserved words: int, void, return.

The other types of words are identifiers that come into two varieties: standard and user defined.

Standard Identifiers:

Standard identifiers have a special meaning in C++. They are the names of operations defined in the standard C++ .

For example: cout is the name of an I/O operation.

Unlike reserved words, standard identifiers can be redefined and used by the programmer for other purposes. However, this is NOT recommended.

User-defined Identifiers:

We choose our own identifiers (called user-defined identifier) to name memory cells that will hold data and program results and to name operations that we define. These names should follow specific rules to be acceptable to the C++ compiler. The rules that make up for a valid identifier are:

ƒ An identifier must consist of only letters, digits and . ƒ An identifier cannot begin with a digit ƒ A C++ reserved word cannot be used as an identifier. ƒ An identifier defined in the C++ should not be redefined. ƒ Some C++ compilers only consider identifiers up to 31 characters.

Uppercase and Lowercase:

C++ is case sensitive. This means that it distinguishes between small and capital letters. Therefore, the identifiers: number, NUMBER and Number are viewed by the compiler as different.

Choosing identifier names:

-Pick a meaningful name for a user-defined identifier. For example, the identifier salary is a good name for a memory cell used to store a person’s salary.

-If an identifier consists of two or more words, placing an character (_) between words will improve the readability. For example: dollar_per_hour rather than dollarperhour.

-Choose good abbreviations for example lbs_per_sq_in is better than pounds_per_square_inch.

The following is a list of valid identifiers:

Introduction,_license_plate, k,TX23,color_of_car

Identifiers in the following list invalid: value$final,license number 3Musketeers,char,hi!!!there,int

Another C++ program:

/*Double program*/

#include

using namespace std;

int main(void)

{ int number,number_2; cout<<“Enter the number\n”; cin>>number; number_2 = number*2; cout<<“The double of”<< number <<”is \n”<

}

The line: int number,number_2; is a declaration. The words “number”, “number_2” are the names of variables.

A variable is a name associated with a memory cell whose value can be changed by a program.

The variable declarations in a C++ program communicate to the C++ compiler the names of all variables used in a program.

They also tell the compiler what kind of information will be stored in each variable.

C++ requires every variable to be declared before its use.

The declaration of variables is done after the opening brace of main()

Every variable must have a name, a type and a value.

General format:

int variable_list1; double var1, var2, variable_list2;

char variable_list3;

EXAMPLES:

int count, large;

double x, y, z;

char first_initial, answer;

The format for declaring variables is

data_type var1, var2, ... ; where data_type is one of the four basic types:

1. int, 2. float, 3. double 4. char 5. bool

Data Types:

Because different variables are used to store different kinds of values, the type of each variable must be specified.

A data type of a variable will determine the set of values that it can take and the set of operations on those values.

Data type int:

In math, integers are whole numbers. The int data type is used to represent integers in C++. The range of data type int includes the values from - 32767 through 32767.

We can store an integer in a type int variable, perform the common arithmetic operations and compare two integers.

Example: -10500 435 -23 +12 Data type float:

A real number has an integral part and a fractional part, which are separated by a decimal point.

For example: 3.14159 0.0005 170.6

We can store a decimal number in a type float variable.

The integral part and the fractional part are separated by a .

Data type double:

A real number can also be stored in a type double variable. For example :

1.2345 0.00005 120000000.0 345.45

For very large or very small numbers, we can use the C++ scientific notation.

In normal scientific notation, 1.23 x 105 is equivalent to 123000.0, where the exponent 5 means move the decimal point five positions to the right, or multiply the number by 100000.

If the exponent has a minus sign then the decimal point is moved to the left, for example: 34 x10-3 is 0.034.

In C++, Scientific Notation, we use: 1.23e5 or 1.23E5; where e or E is read as “times 10 to the power”.

If we need to represent a number with an exponent that has a minus sign, like:

34 x10-3 ,we write as 34e-3.

Data type char: This data type is used to represent an individual character value- a letter, a digit or a special .

For example: ‘A’, ‘a’, ‘z’, ‘2’,’0’, ‘*’, ‘:’, ‘ ‘

If we assign a char value in a C++ program to a type char variable, we are required to put apostrophes, like: char a =’s’

However, if the program is reading in a char value using an input operation like cin >>, then the user should not put apostrophes. cin<< a Æ at execution the user should enter a and not ‘a’

Data type bool:

This data type is used to represent boolean variables. A boolean variable can take two values: true or false. true and false are reserved words and are called logical or Boolean values. The purpose of this data type is to manipulate logical (Boolean) expression

Input/Output Operations and Functions:

Data can be stored in memory in two different ways:

• either by assignment to a variable or

• by copying the data from an input device into a variable using an input function like cin

You copy data into a variable if you want a program to manipulate different data each time it executes. This data transfer from the outside world into memory is called an input operation.

During execution, a program performs computations and stores the results in memory. These program results can be displayed to the program user by an output operation. All input/output operations are performed by special functions called input/output functions.

The cout function: Used to communicate with the user or see the results of a program execution. General format:

function name cout<

We need to call the function cout to display a line of program output. A function call consists of two parts: the function name and the arguments. The cout statement: cout<<“Enter the number\n”; displays the line: Enter the number and positions the cursor in a new line. int time =2; cout<<“The time is ” <

This line will display:

The time is 2 hours The following statement: cin>> number; The cin function reads data from the standard input or the keyboard and copies it into the variable number. General format: cin >> var1 >> var2; function name read list

The function cin >> takes one argument:

The second argument of cin >> begins with an a variable name. This tells cin >> the location in memory in which the variable number is stored. When the computer executes cin >>, it waits for the user to enter a value for variable number.

The user responds by typing an integer and pressing This sends this integer to the computer. The computer assigns this integer value to the variable number. Any reference in the program to the variable number will use this same value.

For example: if the user enters 43 at the prompt, we have: number 43

Assignment Statement:

The statement number_2 = number*2; is an assignment statement:

1- It calculates the product (number x 2), using the operator * for the multiplication

2- it assigns the result of the operation to the variable number_2 using the operator =

General forms of assignment: Case1 var = constant; For example: var1 = 0; This statement assigns the value 0 to the variable called var1.

Case 2: var= variable; For example: var2=var1; This statement assigns the value of var1 to the variable 0. When this statement is executed, the value of var2 is 0.

Case 3: var = expression;

For example var3 = var1+ var2+1;

This statement evaluate the expression var1+ var2+1; and assigns its value to var3

An assignment statement stores a value or the result of a computation in a variable. Assignments are used in most arithmetic operations in C++.

In C++, = is the assignment operator. It is read as “becomes” “gets”, “takes the value” rather than “equals” because it is NOT equivalent to the “equal sign” of mathematics.

In mathematics, this symbol states a relationship between two values, but in C++, it represents an action to be performed.

The assignment operation executes from right to left, which means that the expression at the right of the operator = is evaluated first, Then its value is stored in the variable on the left of the operator = For example: int a = 10+4 1- The addition 10 + 4 is first executed 2- The result 14 is stored in the memory cell called a.

The assignment statement is NOT commutative a = b is different from b = a For example, we have: int a = 2, b= 0; a = b After the assignment a = b; a = 0, b = 0. After the assignment b = a; a = 2, b = 2.

Therefore, the operations have a different effect.

The operand at the left of the assignment operator = should always be a single variable. We cannot have a+b =4

Common Errors:

2= a is WRONG and should be a = 2; x+y = z is WRONG and should be z= x+y;

The statement: cout<<“The double of” << number << “is” << number_2;

The first argument is a string of characters

"The double of” is printed. When the first variable name is reached, the next argument to the cout function (number)is looked up and its value is inserted in the string in the place of the name of the variable.

After inserting the value of the variable number in the output string, the second string “is” is then printed and the variable corresponding to number_2 of the cout is looked up and its value inserted in the string.

The \n is then executed which positions the cursor on the next line.