Lab # 02 Data Types
Lab # 02 Data Types
Type of Data: Data types determine whether a piece of data is an integer, a floating-point
number, a character, a boolean (true/false), or some other type.
Size and Storage Requirements: Data types have associated memory sizes that determine
how much memory space is needed to store a value of that type. For example, an integer
typically requires 4 bytes of memory.
Range of Values: Each data type has a range of values it can represent. For instance, a 32-
bit integer can represent values from -2,147,483,648 to 2,147,483,647.
Operations: Different data types support specific operations. For instance, you can
perform arithmetic operations on numeric types but not on characters.
Data types in C++ can be classified into three main categories: primary (or fundamental), derived,
and user-defined. Here's a breakdown of each category:
Primary (Fundamental) Data Types
These data types are provided by the C++ language itself and are the most basic building blocks
for data representation. They include:
Integer Types: int, short, long, long long
Floating-Point Types: float, double, long double.
Character Types: char, wchar_t, char16_t, char32_t.
Boolean Type: bool.
Void Type: void.
Derived Data Types
Derived data types are created by combining one or more primary data types. They include:
Array Types: Arrays are collections of elements of the same data type.
Pointer Types: Pointers hold memory addresses and are used for dynamic memory
allocation and manipulation.
Reference Types: References provide an alias for an existing object of a specific data type.
User-Defined Data Types
These data types are defined by the programmer and are based on primary and derived data
types. They include:
Structures (struct): User-defined aggregate data types that group together variables of
different data types.
Classes: Similar to structures, but also include member functions and support object-
oriented programming.
Enums (enumerated types): User-defined data types with named integer constants.
Typedefs: Alias created for existing data types to provide more meaningful names (e.g.,
typedef int myInt).
Integer type
Integers are used to store whole numbers. The actual size of a data type can vary between different
systems and compilers. The below given size and range are for 32-bit machine.
Data Type Size(bytes) Range
int 4 -2,147,483,648 to 2,147,483,647
unsigned int 4 0 to 4,294,967,295.
short int 2 -32,768 to 32,767
unsigned short int 2 0 to 65535
long int or signed long int 4 -2,147,483,648 to 2,147,483,647
unsigned long int 4 0 to 4,294,967,295
Floating point type:
Floating types are used to store real numbers. Size and range of Integer type on 32-bit machine
Type Size(bytes) Range
Float 4 3.4E-38 to 3.4E+38
double 8 1.7E-308 to 1.7E+308
Character type
Character types are used to store characters value. Size and range of Integer type on 32-bit machine
Type Size(bytes) Range
char or signed char 1 -128 to 127
unsigned char 1 0 to 255
The size of all data types can be verified using the following C++ code:
#include <iostream>
#include <cstdint>
using namespace std;
int main() {
cout << "Size of short: " << sizeof(short) << " bytes" << endl;
cout << "Size of int: " << sizeof(int) << " bytes" << endl;
cout << "Size of long: " << sizeof(long) << " bytes" << endl;
cout << "Size of long long: " << sizeof(long long) << " bytes" <<
endl;
cout << "Size of float: " << sizeof(float) << " bytes" << endl;
cout << "Size of double: " << sizeof(double) << " bytes" << endl;
cout << "Size of long double: " << sizeof(long double) << " bytes"
<< endl;
return 0;
}
void Type
In C++, the void type is a special data type that is used to indicate the absence of a specific type.
It is often used in various contexts to represent functions or pointers to functions that do not return
a value or to indicate that a function does not accept any arguments.
Datatype of Variable
Here's the basic syntax to declare and define a variable in C++:
data_type variable_name; // Declaration
variable_name = value; // Definition and initialization
Let's break down the components:
1. Data Type: This specifies the type of data the variable can hold, such as int, double, char,
or a user-defined data type.
2. Variable Name: This is an identifier that you choose to name the variable. It should follow
the rules for variable names in C++ (e.g., it should start with a letter or underscore, not be
a C++ keyword, and not contain spaces).
3. Value: The optional value that you can assign to the variable when it's defined. This is
known as initialization. If you don't provide an initial value, the variable contains garbage
data until you assign a value.
int main() {
// Char data type
char myChar = 'A';
cout << "Character: " << myChar << endl;
// Char operations
char nextChar = myChar + 1;
cout << "Next character: " << nextChar << endl;
// Char input
char userInput;
cout << "Enter a character: ";
cin >> userInput;
cout << "You entered: " << userInput << endl;
return 0;
}
ASCII is a character encoding standard that represents characters using numeric codes. For
example, in ASCII:
The character 'A' is represented by the numeric code 65.
The character 'a' is represented by the numeric code 97.
The character 'B' is represented by the numeric code 66.
The character 'b' is represented by the numeric code 98.
You can store characters in char variables, and you can also perform operations on these characters
using their ASCII codes.
Lab Task # 02
Write the following C++ code, you will observe the value and size of data type (in bytes) in
output.
#include <iostream>
using namespace std;
int main() {
double e = 2.71828;
cout << "Double data type: " << e << " (Size: " << sizeof(e) << "
bytes)" << endl;
return 0;
}
Lab Task # 03
1. Write a C++ Program to get an integer and a float number from user and add those number
and display the result.
Hint: In this program, an integer and a float is declared and its value is entered by user using cin.
The values will be added and displayed at output using cout.
Observations/Comments/Explanation of Results
(Please write in your own words the objectives and yours learning during the experiment. Also
explain the results and comment on it.)