Data types, variables and variable assignment

Previous Index Next

Variables Simple data types User-defined data types Variable assignment Mixed-mode assignment

Variables Top

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 abcABC,   AbcAbC, etc. are different.

Declaring variables

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.

Simple data types Top

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

Notes

  1. char is usually used as a character data type (equivalent to char in Pascal), and normally char variables will be displayed as a character. However, it can also be treated as integral data type. See an example here.
  2. A character is enclosed by a pair of single quotation marks ('). For example,'b'.
  3. int and unsigned int (or unsigned) types have the length of the system word. In MS-DOS and 16-bit versions of Windows, their lengths are 16 bits (same as short and unsigned short respectively). In 32-bit operating systems, their lengths are 32 bits (same as long and unsigned long respectively).
  4. There is no Boolean data type in C++. "True" and "false" are implemented as a non-zero value and zero respectively.
  5. For simplicity, int and float in C++ will be considered as equivalent to integer and real in Pascal.

User-defined data types Top

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).


Variable assignment Top

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;

Mixed-mode assignments Top

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.

Program 2: Mixed-mode assignment
#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.

Previous Index Next