Variables and Data Types in C++
In C++, variables are used to store data values, and each variable must be
associated with a specific data type. The data type determines what kind of data
the variable can hold (e.g., integers, floating-point numbers, characters, etc.).
1. Declaring Variables
To declare a variable in C++, you need to specify the data type followed by the
variable name. Optionally, you can assign a value at the time of declaration.
Syntax:
data_type variable_name;
For example:
int age; // Declares an integer variable 'age'
double weight; // Declares a double variable 'weight'
char grade; // Declares a character variable 'grade'
You can also initialize a variable at the time of declaration:
int age = 25; // Declares 'age' and initializes it to 25
double weight = 60.5; // Declares 'weight' and initializes it to 60.5
2. Data Types in C++
C++ supports various data types, which can be broadly classified into primitive
data types and derived data types.
Primitive Data Types
These are the basic types of data that C++ can directly handle.
, 1. Integer Types:
o Used to store whole numbers (without decimals).
o Common types:
int: Used to store integer values, typically 4 bytes.
short: Used for smaller integer values, typically 2 bytes.
long: Used for larger integer values, typically 8 bytes.
long long: Even larger integers.
o Example:
int num = 100;
short smallNum = 10;
long bigNum = 100000L;
2. Floating Point Types:
o Used to store real numbers (numbers with decimals).
o Common types:
float: Typically used for storing single precision floating-point
numbers (4 bytes).
double: Used for double precision floating-point numbers (8
bytes).
long double: Even higher precision (varies based on platform).
o Example:
float temperature = 36.6f;
double pi = 3.14159;
long double preciseValue = 1.12345678912345;
3. Character Type:
o char: Used to store single characters (1 byte).
o Example:
char letter = 'A'; // Stores the character 'A'
4. Boolean Type:
o bool: Used to store boolean values, true or false.
o Example:
bool isAlive = true;
bool hasPassed = false;
In C++, variables are used to store data values, and each variable must be
associated with a specific data type. The data type determines what kind of data
the variable can hold (e.g., integers, floating-point numbers, characters, etc.).
1. Declaring Variables
To declare a variable in C++, you need to specify the data type followed by the
variable name. Optionally, you can assign a value at the time of declaration.
Syntax:
data_type variable_name;
For example:
int age; // Declares an integer variable 'age'
double weight; // Declares a double variable 'weight'
char grade; // Declares a character variable 'grade'
You can also initialize a variable at the time of declaration:
int age = 25; // Declares 'age' and initializes it to 25
double weight = 60.5; // Declares 'weight' and initializes it to 60.5
2. Data Types in C++
C++ supports various data types, which can be broadly classified into primitive
data types and derived data types.
Primitive Data Types
These are the basic types of data that C++ can directly handle.
, 1. Integer Types:
o Used to store whole numbers (without decimals).
o Common types:
int: Used to store integer values, typically 4 bytes.
short: Used for smaller integer values, typically 2 bytes.
long: Used for larger integer values, typically 8 bytes.
long long: Even larger integers.
o Example:
int num = 100;
short smallNum = 10;
long bigNum = 100000L;
2. Floating Point Types:
o Used to store real numbers (numbers with decimals).
o Common types:
float: Typically used for storing single precision floating-point
numbers (4 bytes).
double: Used for double precision floating-point numbers (8
bytes).
long double: Even higher precision (varies based on platform).
o Example:
float temperature = 36.6f;
double pi = 3.14159;
long double preciseValue = 1.12345678912345;
3. Character Type:
o char: Used to store single characters (1 byte).
o Example:
char letter = 'A'; // Stores the character 'A'
4. Boolean Type:
o bool: Used to store boolean values, true or false.
o Example:
bool isAlive = true;
bool hasPassed = false;