2 1 CS C1 0 1 T – OBJ E CT O R IEN TE D DES IG N A N D P R O G RA MM IN G
BASIC DATATYPES/ NATIVE TYPES:
Data types are used to indicate a specific value. When programming, we store the variables
in our computer's memory, but the computer has to know what kind of data we want to store in
them, since it is not going to occupy the same amount of memory to store a simple number than to
store a single letter or a large number, and they are not going to be interpreted the same way.
The memory in our computers is organized in bytes. A byte is the minimum amount of
memory that we can manage in C++. A byte can store a relatively small amount of data: one single
character or a small integer (generally an integer between 0 and 255). In addition, the computer
can manipulate more complex data types that come from grouping several bytes, such as long
numbers or non-integer numbers. Data types in c++ can be of seven basic C++ data types.
Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t
Several of the basic types can be modified using one or more of these type modifiers:
• signed
• unsigned
• short
• long
The following table shows the variable type, how much memory it takes to store the value
in memory, and what is maximum and minimum value which can be stored in such type of
variables.
Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
Page No: 6
2 1 CS C1 0 1 T – OBJ E CT O R IEN TE D DES IG N A N D P R O G RA MM IN G
long int 4bytes -2,147,483,647 to 2,147,483,647
signed long int 4bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character
Following is the example, which will produce correct size of various data types on your computer.
using namespace std;
#include <iostream>
main( )
{
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
}
Output:
Size of char : 1
Size of short int : 2
Size of float : 4
Size of double : 8
Size of wchar_t : 4
This example uses endl, which inserts a new-line character after every line and
sizeof() function to get size of various data types.
typedef Declarations:
New name for an existing type can be created using typedef. Following is the simple syntax
to define a new type using typedef:
typedef Datatype variableName;
For example, the following tells the compiler that feet is another name for int:
typedef int feet;
Now, the following declaration is perfectly legal and creates an integer variable called distance:
feet f1,f2;
Enumerated Types:
An enumerated type declares an optional type name and a set of zero or more identifiers
that can be used as values of the type. Each enumerator is a constant whose type is the
enumeration. To create an enumeration requires the use of the keyword enum. The general form
of an enumeration type is:
Page No: 7
2 1 CS C1 0 1 T – OBJ E CT O R IEN TE D DES IG N A N D P R O G RA MM IN G
enum enumName {list of names} Variable;
Where,
• The enum is an Keyword.
• enum-name is the enumeration's type name.
• The list of names is comma separated.
For example, the following code defines an enumeration of colors called colors and the
variable c of type color. Finally, c is assigned the value "blue".
enum color { red, green, blue} c;
c=blue;
By default, the value of the first name is 0, the second name has the value 1, the third has
the value 2, and so on. But we can give a name a specific value by adding an initializer. For example,
in the following enumeration, green will have the value 5.
enum color { red, green=5, blue} c;
c=blue;
Here, blue will have a value of 6 because each name will be one greater than the one that precedes
it.
============================================================================
Tokens:
The smallest individual units in a program are known as tokens. C++ has the following
tokens:
• Keywords
• Identifiers
• Constants
• Operators
• Strings
(a) Keywords:
They are explicitly reserved identifiers and cannot be used as names for the program
variables or other user-defined program elements.
(b) Identifiers
Identifiers refer to the names of variables, functions, arrays, classes, etc. created by the
programmer. They are the fundamental requirement of any language. Each language has its own
rules for naming these identifiers. The rules for declaring an identifier are as follows:
• Identifier name must start with alphabetic characters, digits or underscore.
• Identifier name cannot start with a digit.
• Uppercase and lowercase letters are treated as different.
• Keywords should not be used as an identifier.
• No space must be present between the identifier name.
(c) Constants
Page No: 8
2 1 CS C1 0 1 T – OBJ E CT O R IEN TE D DES IG N A N D P R O G RA MM IN G
Constants are expressions with a fixed value. Constants can be divided into Integer
Constants, Floating-Point Constants, Character Constants, String Constants and Boolean Values.
(i) Integer Numerals are numerical constants that identify integer decimal values.
Example: 1776, 707, -273
In addition to decimal numbers C++ allows the use as Integer constants of octal numbers
(base 8) and hexadecimal numbers (base 16). If we want to express an octal number we have to
precede it with a 0 (zero character). And in order to express a hexadecimal number we have to
precede it with the characters 0x (zero, x). For example, the following literal constants are all
equivalent to each other:
75 // decimal
0113 // octal
0x4b // hexadecimal
(ii) Floating Point Constants are numbers with decimals and/or exponents. They can include
either a decimal point, an e character or both a decimal point and an e character.
Example:
3.14159 // 3.14159
6.02e23 // 6.02 x 10^23
1.6e-19 // 1.6 x 10^-19
3.0 // 3.0
(iii) Character Constants - Character Constants are enclosed within single quotes ( ‘ ‘ )
Example: 'z', ‘&’, ‘G’
(iv) String Constants - String Constants are enclosed within double quotes ( “ “ )
Example: “Hello”, “EEE Depart”
Escape codes/Escape Sequence - These are special characters that are difficult or impossible to
express otherwise in the source code of a program, like newline (\n) or tab (\t). All of them are
preceded by a backslash (\). Some of the escape codes:
\n newline
\r carriage return
\t Tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)
(v) Boolean literals - There are only two valid Boolean values: true and false. These can be
expressed in C++ as values of type bool by using the Boolean literals true and false.
Page No: 9
2 1 CS C1 0 1 T – OBJ E CT O R IEN TE D DES IG N A N D P R O G RA MM IN G
Declared constants (const)
With the const prefix we can declare constants with a specific type in the same way as
variables aredeclared.
Example: const float PI=3.14;
Here, “PI” is an typed constants. It was used like regular variables except it value cannot be
modified after the definition.
Variables:
In order to store the value variable is needed. All variables must be declared before they
may be used in the program.
Syntax: Datatype Variable Name1,Variable Name2, ….., Variable NameN;
The following statement uses four variables numberl, number2, sum, and average. They are
declared with datatype float.
float number 1, number 2, sum, and average;
Scope of variables
A variable can be either of global or local scope. A global variable is a variable declared in
the main body of the source code, outside all functions, while a local variable is one declared within
the body of a function or a block. Global variables can be referred from anywhere in the code, even
inside functions, whenever it is after its declaration. The scope of local variables is limited to the
block enclosed in braces ({ }) where they are declared. For example, if they are declared at the
beginning of the body of a function their scope is between its declaration point and the end of that
function.
Initialization of variables
When declaring a regular local variable, its value is by default undetermined. But if we
want a variable to store a concrete value at the same moment that it is declared. We must initialize
the variable. There are two ways to do this in C++:
The first one, known as c-like, is done by appending an equal sign followed by the value to
which the variable will be initialized:
Syntax: Datatype VariableName = initial_value ;
Example: int a = 0
The other way to initialize variables, known as constructor initialization, is done by
enclosing the initial value between parentheses (()):
Syntax: Datatype VariableName (initial_value) ;
Example: int a(0);
Both ways of initializing variables are valid and equivalent in C++.
(d) Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C++ is rich in built-in operators and provides the following types of operators:
Page No: 10