The document provides an introduction to C++ variables, data types, and constants, explaining that a variable is a memory location labeled with a user-defined name for storing values. It details data types, including built-in types like int, char, and float, and emphasizes the importance of declaring variables for data classification and proper storage. Additionally, the document discusses variable initialization, constant declarations, and best practices for managing data types in programming.
Variables A variableis a memory location that is used to store values . To understand what this means let us take an example. Consider the memory of the computer to contain of equal sized cells capable of storing data. These cells have unique addresses. Since we store data in these we need to refer to them in our program. It is humanly impossible to remember all the addresses
So We canname these Memory locations with some user defined names(identifier) like we have named this one as age. Here age is the name given to memory location AFXX011 and is called the variable age. We shall see how to declare a variable discussing data types age
5.
How do weuse Variables ? In order to be able to use the memory location as a variable we need to declare it ie allocate memory to it. A variable can be declared using a data type and a valid identifier. Let us first discuss data types.
6.
What is aData Type? A data type defines a set of values and the operations that can be performed on that data. Data Operations Data Type
7.
Why do weneed a data type? As all of you must be aware that a computer is just a machine. It cannot by itself distinguish between various types of Data. This means that it cannot distinguish between the number 20 and the letter ‘A’ or the word “good” . For the computer all of this is just a piece of data. It is the programmer who must tell the computer that 20 is a number, ‘A’ is a character and ‘good’ is a word. How do we do it? By using data types we can classify the different data for the computer so that it can be stored and processed in a certain manner.
8.
C++ Data Types: Categories C++ Data Types In-Built int, char, float, void User Defined Classes , structures Derived Arrays The C++ data types can be categorized as
9.
C++ Data Types:In-built C++ supports four basic data types : Basic Data types int char float void
10.
void The void typehas no values and no operations. In other words, both the set of values and the set of operations are empty. Although this might seem unusual, we will see later that it is a very useful data type.
11.
Characteristics of datatypes All data types have a certain size associated with them. This essentially means that data of each type has to be stored in a certain no of bytes. Each data type has a range of permissible values associated with it which is also its domain. All data type have some modifiers to accommodate various ranges for eg int data type has short long
12.
int data type Variablesdeclared as integers are capable of storing whole numbers These are numbers without decimal point. There are three variants of integer type. These differ in size. The following is a table showing the size and domain of integer data types. Type modifier Size (byte s) Min value Max value Sample data (To Store) short int Signed Unsigned 2 -32768 0 32767 65535 Marks Age int signed unsigned 2 -32768 0 32767 65535 long int signed unsigned 4 -2,147,483,648 0 2,147,483,647 4,294,967,295 Population
13.
Floating Point A floating-pointvariable is capable of storing a number with a fractional part. The C++ language supports three different sizes of floating-point: float, double and long double. Type Size (byt es) Min value Max value Sample data (To Store) float 4 3.4 E -38 3.4 E 38 Average double 6 1.7 E -308 1.7 E 308 Huge fractional calculation Long double 10 3.4 E -4932 3.4 E 4932 Stellar distances
14.
Char data type Achar variable is capable of storing any character on the keyboard. The ASCII code of the character is stored.eg the symbol ‘+’, the letter ‘A’ etc. Type Size (byt es) Domain Sample data (To Store) char 1 Any character on the keyboard Symbol operator
15.
Variable declarations A variabledeclaration specifies the type and the name of the variable. A variable has a type and it can contain only values of that type. For example, a variable of the type int can only hold integer values. Syntax: data type identifier ; //declaration data type identifier = value; // initialization Examples: int age; int no_of_books = 45; char letter= 'y'; double price = 2493.14; float temp = -24.5;
16.
Variable declarations Wheninitializing a constant or a variable of char type, or when changing the value of a variable of char type, the value is enclosed in single quotation marks. Examples: const char star = '*'; char letter = ‘D';
17.
Declaration vs Initialization When a variable is given a value at the time of declaration itself this is known as initialization int num; int num=89 Variables are not automatically initialized. For example, after declaration int sum; the value of the variable sum can be anything (garbage). Thus, it is good practice to initialize variables when they are declared.
18.
Constant declarations Constantsare used to store values that never change during the program execution. Using constants makes programs more readable and maintainable. Syntax: const data type identifier = value; Examples: const double rate = 7.8; const int x= 45;