In C++, variables must be declared before they are used. Like Pascal, a variable name consists of letters, digits and underscores, and it cannot begin with a digit. However, C++ is case sensitive. Therefore the variables abc, ABC, Abc, AbC, etc. are different.
Declarative statements can appear anywhere in the program, provided that the declaration of a variable must appear before the variable is used in the program.
A declarative statement starts with the type of the variable(s), followed by the variable name(s), and ends with a semicolon (;). If more than one variable is declared in a declarative statement, we use commas (,) to separate them.
Examples
int x; // x is an integer variable. float a1, a2; // a1 and a2 are floating-point variables.
The following table shows some data types in C++:
C++ type names | Other C++ type names | Length | Range | Equivalent Turbo Pascal data type |
int | signed signed int |
System dependent (see Notes) | - | |
unsigned int | unsigned | System dependent (see Notes) | - | |
char (see Notes) | signed char | 8 bits | -128 to 127 | shortint |
unsigned char | - | 8 bits | 0 to 255 | byte |
short | short int signed short int |
16 bits | -32768 to 32767 | integer |
unsigned short | unsigned short int | 16 bits | 0 to 65535 | word |
long | long int signed long int |
32 bits | -2147483648 to 2147483647 | longint |
unsigned long | unsigned long | 32 bits | 0 to 4294967295 | - |
float | - | 32 bits | 3.4E-38 to 3.4E+38 | single |
double | - | 64 bits | 1.7E-308 to 1.7E+308 | double |
long double | - | 80 bits | 3.4E-4932 to 1.1E+4932 | extended |
We can use typedef to define new data types. For example:
C++ statements | Equivalent Pascal statements |
typedef int Length; Length x; |
type Length = integer; var x: Length; |
typedef is useful for defining structured data types (especially records).
The assignment operator used in C++ is "=". It is equivalent to ":=" in Pascal.
Note that in C++, "=" does not mean "equal to". Use "==" (two equal signs) as "equal to" in C++.
Also, initialisation of a variable can be done in the declarative statement. For example, the following two C++ program segments are the same.
C++ statements | Equivalent Pascal statements |
int x; x = 4; |
var x: integer; ... x := 4; |
int x = 4; |
We can also declare and initialise more than one variable in one statement. Moreover, it is possible that we do not initialise all the variables in a declarative statement:
C++ statements | Equivalent Pascal statements |
char ch1 = 'a', ch2 = 'A'; | var ch1, ch2: char; ... ch1 := 'a'; ch2 := 'A'; |
int i = 4, j, k = 3; // j is not initialised |
var i, j, k: integer; ... i := 4; k := 3; |
In an assignment statement where the operands on the two sides of the assignment operator (=) are different (mixed-mode assignment), the value of the right hand side will be coerced to the data type of the variable on the left hand side before it is stored. In particular:
The following program demonstrates mixed-mode assignments.
#include <iostream.h> main() { int i; // i is an integer variable. float x; // x is a floating-point variable. char c; // c is a character variable. x = 4.7; // 4.7 is assigned to x. cout << x << endl; i = x + 2.1; // 6 (after truncating the fractional part of ... cout << i << endl; // ... 6.8) is assigned to i. c = 81; // The character with ASCII code 81 ('Q') is ... cout << c << endl; // ... assigned to c. The character is 'Q'. i = 'b'; // The ASCII code of 'b' (98) is assigned to i. cout << i << endl; return 0; } |
More about assignment operators will be discussed here.