C++ Variables Types
In C++, a memory location is referred to as a variable. It is the essential data storage component of a program.
- A program's variables can have their values changed while it is running.
- A variable's value is nothing more than the name given to a memory region; all operations on the variable affect that memory address.
- In C++, each variable needs to be declared before it is used.
How Should Variables be declared?
A standard variable declaration looks like this:
//declaring one variable of
type variable_name;
// Declaring multiple variables of the
types variable1_name, variable_name, and variable3_name.
Uppercase and lowercase letters from the alphabet, digits, and the underscore character ('_') can all be used to create variable names.
This flexibility enables developers to give their variables expressive and insightful names, improving readability and comprehension of the codebase.
- To guarantee that the variable names are correct and consistent with the programming language being used, it is necessary to follow certain rules and norms.
- Datatype: The type of data this variable can store.
- Variable_name: The variable's name.
- Value: This is the variable's starting value.
Rules Specifying Variables:
- The variable's name consists of underscores, numbers, and letters.
- The variable name is case-sensitive (for instance, the variables Arr and arr are distinct).
- There are no whitespace or special characters (such as #,$,%, or *) in the variable's name.
- We cannot use a C++ keyword as a variable name, such as float, double, or class.
Variable types:
- Local variables
- Instance variables
- Static Variables
Let's study more about each of these factors individually now.
Local Variables: In a programming language, local variables are variables that are declared and specified inside a particular piece of code, method, and constructor.
- Since they are created when the block is entered or when the method or constructor is invoked and are erased from memory when the block is left
- When the method or constructor completes its operation, these variables have a finite scope and lifetime.
- The local variables are produced every time the block is entered or the method or constructor is used again.
Instance variable: Variables declared inside a class but not inside any methods, constructors, or blocks are referred to as instance variables, often known as non-static member variables.
- They are associated with a particular instance of the class and are assigned a unique value for every object derived from that class.
- When a class object is created, these variables are initialised and deleted when the object is no longer needed or is destroyed.
Static Variables:
A variable becomes a static member variable when it is specified as static inside of a class. Shared among all instances (objects) of the class are the static member variables. All objects of the class have access to the same variable's memory address because they are only initialised once.
- The value of a variable that has been declared static inside of a function is carried over from one function call to the next.
- This indicates that the variable only needs to be initialised once, upon calling the function for the first time, and it retains its value after that.
Other Types:
Integer Types: Whole numbers without fractional components are stored in integer types.
They come in two varieties:
- Signed (representing both positive and negative numbers)
- Unsigned (representing only non-negative values).
Int: Usually a signed 32-bit integer.
Short: Usually a signed 16-bit integer.
Long: Usually, a signed integer is 32 or 64 bits long.
Long long: Usually a signed 64-bit integer.
Unsigned int: A 32-bit unsigned integer is typical.
Unsigned short: A 16-bit unsigned integer, typically.
Unsigned long: A 32-bit or 64-bit unsigned integer is the most common size.
Unsigned Long Long: A 64-bit unsigned integer, typically.
Floating-Point Types
Used to store fractional parts of numbers. Float, double, and long double are a few of them. With differing degrees of precision, these types can represent both small and large decimal numbers:
Float: A 32-bit floating-point number in most cases.
Double: A 64-bit floating-point number typically.
Long double: A more significant floating-point number typically.
Individual characters are stored using character types
Characters
Typically stored as an 8-bit type called char. It can display ASCII character set characters.
Char: A single 8-bit character, typically Individual characters are stored using character types.
wchar: A wide character that may typically represent a wider variety of characters.
Char: A 16-bit character that is frequently used for Unicode characters.
Char: A 32-bit character that is frequently used for Unicode characters
Boolean Type: Used to store values that are true or false.
Enumeration: Enumeration types, usually called enums, specify named constants. A technique to give meaningful names to a group of connected values is through enums.
To illustrate, list the names as follows:
enum Languages:{C, C++, Java, Python, C#, Ruby, Pascal};
In this example, the enum Languages are defined, and the variable languages used are declared with C++,c, java, and Python as its value. Your code may become easier to comprehend and maintain with enumerations.
Array: Memory addresses are stored using pointer types.
Type* A reference to a type* value.
Using array types, multiple values of the same type can be saved.
Types of Arrays: Sorts of Arrays When using arrays, you can keep similar values next to one another. An array's size is chosen during compilation.
For instance:
Declares an array of 5 integers
int numbers[5];
A collection of values can be worked with effectively using arrays, and elements can be accessed using indices.
Structures and Classes: By merging variables of various types into a single unit, structures, and classes enable you to construct bespoke data types. They may also have member functions for behavior encapsulation.
This is an illustration of a structure:
struct Person {
std::string name;
int age
}
The Person structure in this example has two members: a string name and an integer age. Classes and structures are necessary for organizing.
Class: An improved form of a structure capable of having member functions.
Variable Declaration (vs)Variable Definition
The variable declaration refers to the code area where a variable is first introduced or defined before it is used. When defined, a variable is given a memory location and a value. Typically, the declaration and definition of variables are done simultaneously.
For further information, view the C++ program after this one:
Example:
#include <iostream>
using namespace std;
int main()
{
//This is a declaration of variable x
int x;
// this is initialisation of x
x = 15;
// this is definition = declaration + initialisation
int y = 25;
// declaration and definition
// of variable 'b456'
char b456 = 'b';
float z;
// multiple declarations and definitions
int _p, _q78, r;
// Let us print a variable
cout << b456 << endl;
return 0;
}
Output:
![C++ Variables Types](https://static.tutorialandexample.com/c/cpp-variables-types1.png)
Declaration: A declaration informs the compiler of a name and its data type. It instructs the compiler that a particular variable with a particular name and data type will be used in the program.
Char b456: Defines a character variable with the name b456 float z declares a floating-point variable with the name z.
- int _p, _q78, r; declares multiple integer variables with the names _p, _q78, and r.
Initialization: Giving a variable its initial value is the process of initialization. After the declaration, it is completed.
Initializations in the program include the following:
- Int y = 25; initializes an integer variable y with 25.
- char b456 = 'b'; sets the character variable b456 to the letter 'b' as its first value.
Definition: Declaration and initialization are combined in a definition. In addition to introducing the variable, it also gives it a starting value.
Importance:
- Variables store data and come in various kinds, including int, float, and user-defined variables.
- A variable can have a value assigned to it when it is defined; otherwise, if no value is assigned, a garbage value is assigned.
- Variable definitions and declarations have different meanings. While the definition also allocates memory, the declaration only provides the data type and names of variables.
- The operation of variables in C++, where the variable type indicates the memory space the variable requires.
- Rules for establishing variables that outline the names of variables that the compiler will permit.