Learn C++ - C++ Variable






A variable is a named piece of memory that you define.

Each variable only stores data of a particular type.

Every variable has a type that defines the kind of data it can store.

Each fundamental type is identified by a unique type name that is a keyword.

Keywords are reserved words in C++.

Names for Variables

You do have to follow a few simple C++ naming rules:

The only characters you can use in names are alphabetic characters, numeric digits, and the underscore (_) character.

The first character in a name cannot be a numeric digit.

Uppercase characters are considered distinct from lowercase characters.

You can't use a C++ keyword for a name.

Here are some valid and invalid C++ names:

int myvalue;    // valid 
int MyValue;    // valid and distinct
int MYVALUE;    // valid and even more distinct 
Int three;      // invalid -- has to be int, not Int 
int my_value3   // valid 
int _Myvalue3;  // valid but reserved -- starts with underscore 

To form a name from two or more words, the usual practice is to separate the words with an underscore character, as in my_onions, or to capitalize the initial character of each word after the first, as in myEyeColor.





The const Qualifier

C++ uses the const keyword to declare constant value.

const int Months = 12;  // Months is symbolic constant for 12 

The general form for creating a constant is this:

const type name = value;

Note that you initialize a const in the declaration.