Identifiers
An identifier is a name of a variable or function in C++. The same basic rules you had in Python exist in C++.
- Can’t be a reserved word/keyword
- Case sensitive (name, Name, and NAME are all different things)
- Can only be used in one context
- Must start with a letter
- May have letters, numbers, and underscores
- Some compilers limit the length of the name
Variables
When you define a variable, you can define multiple variables of the same type at the same time. While legal, not everyone likes to do this as it can make it harder to find the definitions in code.
int i, j, k; // defines three integer variables - legal
By standard convention, most people define variables in lower case, or camel case. Camel Case is where a multi-word name is written as one word, but the first letter of each word, except for the first is capitalized so it is easier to read.
float roomSize;
int countOfAttendees;
You can declare a variable (allocate memory by giving it a value) when you define it (give it a name).
int familySize = 4;
double roomLength = 18.6;
int i(10); // declares an integer named "i" with a value of 10.
// You don't see this a lot.
Constants
Constants are declared when they are defined and cannot change in C++. To define a constant, you use the keyword const before the type. Most people will define the constant name in all upper case to deferentiate it from a regular variable.
const double PI = 3.141592;
Once a constant is declared, you can use it in any equation like you would a variable, as long as you don’t try to assign it a value.
Numeric Data Types
C++ offers various data types. Over the years, the size has increased and is now defined in the C++ and IEEE standards.
- (unsigned) short – a smaller integer – 16bit
- (unsigned) int – 32 bit
- (unsigned) long – – used to be a long , or bigger integer, but now is the same size – 32 bit
- float – 32 bit floating point number
- double – 64 bit floating point number – usually used.
- long double – 80 bit floating point number
C++ Variables and Constants was originally found on Access 2 Learn