C++ Primer For Java Programmers


Variable Declarations

<< | Table of Contents | >>
!! Identifiers

As in all programming languages, we use identifiers to name things such as variables, functions, data types, classes, etc. Identifiers in C++:

  • can consist of characters, digits, and the underscore
  • must begin with a character
  • are case sensitive
  • can be of any length, though the first 32 characters must be unique.

Variables

Variables in C++ must be explicitly declared as they are in Java.

(:source lang=cpp:)
int grade,
    sum,
    total;

double average,
       salary;

In C++, variables are not initialized when they are created. You can declare a variable and initialize it at the same time

(:source lang=cpp:)
int total = 0;
double taxRate = 0.05;

Data Types

C++ provides a number of primitive data types including

Integers
char 1-byte signed
short 2-bytes signed
int size of system word
(4-bytes: 32-bit architecture)
(8-bytes: 64-bit architecture)
long 4-bytes signed
long long 8-bytes signed
(implemented in software)
unsigned char 1-byte unsigned
unsigned short 1-byte unsigned
unsigned short 2-bytes unsigned
unsigned int same size as an int
unsigned int 4-bytes unsigned
unsigned long long 8-bytes unsigned
(implemented in software)
Reals
float 4-bytes
double 8-bytes
long double 12-bytes
Other
bool 4-bytes
enum 8-bytes
reference same size as an int


<< | Table of Contents | >>

Print - Changes - Search
Last modified: April 27, 2007, at 04:24 PM.