0% found this document useful (0 votes)
27 views8 pages

Lab # 02 Data Types

Uploaded by

alizarizwan006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
27 views8 pages

Lab # 02 Data Types

Uploaded by

alizarizwan006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

University of Management and Technology

Department of Artificial Intelligence

CC1021L Programming Fundamentals Lab


Fall 2023
Participant ID: Participant Name:

Date: Total Marks: Obtained Marks:

Lab # 2: Data Types in C++


Learning Objectives
 Understand importance of various data types such as integers, floating-point, character, and
Boolean and understand the importance of type selection.
 Demonstrate proficiency in declaration and initialization of a variable from various data
types in C++.

Data types in C++


Data types in a programming language are a fundamental concept that define the kind of data that
can be stored and manipulated in a program. They specify the following aspects of data:

 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.

Identifier and Variables in C++


In C++, an identifier is a name given to various program elements, such as variables, functions,
classes, objects, and labels. Identifiers are used to uniquely identify and reference these program
elements within the source code.
When we want to store any information (data) on our computer/laptop, we store it in the computer's
memory space. Instead of remembering the complex address of that memory space where we have
stored our data, our operating system provides us with an option to create folders, name them, so
that it becomes easier for us to find it and access it.
Similarly, in C++ language, when we want to use some data value in our program, we can store it
in a memory space and name the memory space so that it becomes easier to access it.
The naming of an address is known as variable. Variable is the name of memory location. Unlike
constant, variables are changeable; we can change value of a variable during execution of a
program. A programmer can choose a meaningful variable name. Example: average, height, age,
total etc.

Difference between Identifier and Variable


Identifier Variable
 An identifier is a name given to various program  A variable is a specific type of program element
elements, such as variables, functions, classes, that represents a named storage location for
and objects. data. Variables hold values of a specific data
 Identifiers are used to uniquely identify and type, such as integers, floating-point numbers,
reference program elements in source code. or objects.
 Identifiers must adhere to naming rules and  Variables are used to store, manipulate, and
conventions in the programming language used. represent data within a program.
 Identifiers are not limited to variables; they can  Variables must be declared and defined before
be used for other elements like functions, they can be used to store data.
classes, and more.  A variable is a named identifier that is
 They help make the code more readable and associated with a particular data type and can
self-explanatory by giving meaningful names to hold a value of that type.
program elements.
Example: Example:
// a variable // int variable
int studytonight; int a;
// or, a function // float variable
int studytonight() float a;
{
..
}

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.

Rules to name a Variable


Here are the rules for naming variables in C++:
 Valid Characters: Variable names can only consist of letters (both uppercase and
lowercase), digits, and underscores. No other special characters or spaces are allowed.
Variable names must begin with a letter or an underscore.
 Case Sensitivity: C++ is case-sensitive, so variable names like myVar and myvar are
considered different variables.
 C++ Keywords: You cannot use C++ keywords (reserved words) as variable names.
Keywords are reserved for specific language features and cannot be redefined. Some
examples of keywords include int, double, for, if, and class.
 Naming Conventions: Two popular conventions are:
o CamelCase: Capitalize the first letter of each word in the variable name, except the
first word (which starts with a lowercase letter). This is commonly used for variable
names, function names, and class names. Example: myVariableName
o snake_case: Separate words with underscores, and use all lowercase letters.
Example: my_variable_name, calculate_total_price()

Variable Declaration, Initialization and Definition in C++


In C++, a variable declaration is a statement that tells the compiler the name and data type of a
variable without necessarily assigning an initial value to it. Variable declarations are important
because they inform the compiler about the type of data the variable will store, allowing the
compiler to allocate memory for the variable.
Here's the basic syntax for variable declaration in C++:
data_type variable_name;
Initialization is the process of providing an initial value to a variable that has already been defined.
Initialization can occur at the time of definition or at a later point in the program.
variable_name = initial_value;
Definition is the process of declaring a variable and also allocating memory for it. It associates
the variable name with a specific memory location and may also provide an initial value.
data_type variable_name = initial_value;
Lab Task # 01
Execute the following program and observe its output. The replace character “A” will the first letter of
your name and observe the output. The char data type stores character based on American Standard
Code for Information Interchange (ASCII) characters.
#include <iostream>
using namespace std;

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;
}

Figure 1. ASCII Codes

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() {

// Bool data type


bool isPassed = true;
cout << "Bool data type: " << isPassed << " (Size: " <<
sizeof(isPassed) << " bytes)" << endl;

// Integer data type


int age = 20;
cout << "Integer data type: " << age << " (Size: " << sizeof(age)
<< " bytes)" << endl;

// Floating-point data types


float pi = 3.14159;
cout << "Float data type: " << pi << " (Size: " << sizeof(pi) <<
" bytes)" << endl;

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.)

Checked By: Date:

You might also like