P R O G RA M M I N G 1
Datatypes,
Variables and
Constant
VARIABLES
What is a variable?
A variable in C++ is a storage location in
memory with a specific name, where data
can be stored, modified, and retrieved during
the execution of a program. The name of a
variable is known as an identifier.
Purpose of a
Variable
Purpose of a variable:
1.Storage of Data: Variables are used to store data that the
program will manipulate.
2.Reusability: By storing data in variables, you can reuse and
manipulate the same data without having to enter it
multiple times.
3.Memory Management: Variables allow efficient use of
memory, enabling dynamic allocation and deallocation as
needed.
4.Readability and Maintainability: Using meaningful variable
Use of a variable:
Variables are used in almost every aspect of
programming, such as storing user input,
performing calculations, or controlling the flow of a
program. For example, if you want to add two
numbers, you store each number in a variable, add
them, and store the result in another variable.
Declare a variable in C++
data_type variable_name;
Different Datatypes
Primitive Data types
Primitive data types are the basic types of data that are built into
the C++ language.
1.int: Represents an integer (whole number).
⚬ Example: int age = 25;
2. short: Represents a short integer, typically smaller in range than
int.
⚬ Example: short temperature = 100;
3.long: Represents a long integer, typically larger in range than int.
⚬ Example: long distance = 123456789;
Primitive Data types
1.float: Single-precision floating-point type
• Example: float height = 5.9f;
1.double: Double-precision floating-point type, offering more precision
than float.
• Example: double pi = 3.141592653589793;
1.char: Represents a single character.
• Example: char grade = 'A';
1.bool: Represents a boolean value, either true or false.
• Example: bool isStudent = true;
1.String: Represents any text
Derived Data types
Derived data types are formed by combining basic data types.
1.Arrays: A collection of elements of the same data type stored in
contiguous memory locations.
• Example: int arr[5] = {1, 2, 3, 4, 5};
1.Pointers: A variable that stores the memory address of another variable.
• int* ptr = &age;
1.References: An alias for another variable.
• Example: int& ref = age;
Derived Data types
Derived data types are formed by combining basic data types.
4. Functions: A block of code that performs a specific task.
• Example: int add(int a, int b);
Basic Structure of C++ This line includes the iostream library, which is necessary for
input and output operations in C++. The iostream library
provides functionalities to read from standard input and
write to standard output.
#include <iostream> This line allows you to use all the entities (like
functions, objects, and variables) in the std
namespace without prefixing them with std::. This
using namespace std; is done to simplify the code and make it more
readable.
This is the main function of the program, which is
the entry point of every C++ program. When the
int main() {
program is executed, the execution starts from this
cout << "Hello, function. The int keyword indicates that this
World!" << endl; function returns an integer value.
cout is an object of the ostream class, used
return 0;
to output data to the standard output.
} This line terminates the main function and returns the The << operator is used to send the string
value 0 to the calling process (usually the operating to the cout object.
system). Returning 0 typically indicates that the
program ended successfully without any errors