You are on page 1of 9

1.

Data types: C++ data types can be classified as


a. Standard data types
b. Derived (extended data types)
c. User defined data types
The following figure represents the detail of C++ data type classification
Standard data types:
Standard data types are used to tell the compiler what type of data to be handled by an
identifier. These data types give complete details of the behavior of a data type. It is
possible to handle only one value at a time. C and C++ compilers supports the built in
data types. With the exception of void the basic data types may have several modifiers
(signed, unsigned, long, short) may apply to the character and integer basic data types.

Void: - The Void is used to represent the following things


1)
To specify the return type of a function when it is not returning any value.
2)
To indicate an empty argument list to a function.
3)
Declaration of the generic pointer.
User defined data types:
User defined data types are created by a user by using simple, derived, user defined
data types as per the user requirement. The user has to decide the functionality and
behavior of these data types. By using these data types to handle more than one data
type using single identifier For example structures and classes
2. Derived data types (extended data types):
Derived data types are used to extend the behavior of an existing simple data type to
get more functionality. It is possible to handle more than one value at a time using a
single identifier. For example arrays.

The following table gives the details regarding various data types.
Data Type
a.
b.
c.
d.
e.
f.
g.
h.
i.
j.
k.

Char
Unsigned Char
Signed Char
int
Unsigned int
Signed int
Short int
long int
float
double
long double

Size in bytes
1
1
1
2
2
2
2
4
4
8
10

Range
-128 to 127
0 to 255
-128 to 127
-32768 to 32767
0 to 65,535
-32768 to 32767
-32768 to 32767
-2147483648 to 2147483647
1.2e-38 to 3.4e38
2.2e-308 to 1.8e308
3.4e-4932 to 1.1e+4932

3. Basic data types in C++ consist of:

Character denoted by char is the data type that holds an integral value
corresponding to the representation of an element of the ASCII character set.
Integer denoted by int is the data type that holds an integer value or a whole
number.
Real denoted by:
o float is the data type that holds a singleprecision floating point value or a
real number; or
o Double is the data type that holds a doubleprecision floating point value
or a real number.
Boolean denoted by bool is the data type that holds a boolean value
of true or false.

Byte is the smallest addressable memory unit. Bit, which comes from BInary digiT,
is a memory unit that can store either a 0 or a 1. A byte has 8 bits. The data type
byte sizes are as follows:

char takes 1 byte


int takes 2 bytes
float takes 4 bytes
double takes 8 bytes

Qualifiers are additional attributes to a data type to possibly change the size and / or
the interpretation of the sign bit of a data type. They are as follows:

Signed which is the default value: Positive or negative values may be assigned
to a variable of type signed.

Unsigned: Only positive values may be assigned to a variable of


type unsigned.
sizeof ( unsigned int ) = sizeof ( int )
2 bytes = 2 bytes

short which is used with int: This may change the size of int.
sizeof ( short int ) <= sizeof ( int ) <= sizeof ( long int )
2 bytes =
2 bytes
< 4 bytes

long which is used with int and double: This changes the size
of int and double.
sizeof ( float ) <= sizeof ( double ) <= sizeof ( long double )
4 bytes <
8 bytes
< 10 bytes

A variable is a valid ID which refers to a memory location where a value, which may
be changed, may be stored for use by a program. All variables must be declared
with a name and a data type at the beginning of a block before they can be
referenced in a statement.
3. Manipulators
Manipulators are operators used in C++ for formatting output. The data is
manipulated by the programmers choice of display.
There are numerous manipulators available in C++. Some of the more commonly
used manipulators are provided here below:
Stream manipulators
Manipulators are functions specifically designed to be used in conjunction with the
insertion (<<) and extraction (>>) operators on stream objects, for example:
cout << boolalpha;
They are still regular functions and can also be called as any other function using a
stream object as argument, for example:
boolalpha (cout);
Manipulators are used to change formatting parameters on streams and to insert or
extract certain special characters.

Basic format flags


These manipulators are usable on both input and output streams, although many only
have an effect when applied to either output or input streams.
Independent flags (switch on):
boolalpha
Alphanumerical bool values (function )
showbase
Show numerical base prefixes (function )
showpoint
Show decimal point (function )
showpos
Show positive signs (function )
skipws
Skip whitespaces (function )
unitbuf
Flush buffer after insertions (function )
uppercase
Generate upper-case letters (function )
Independent flags (switch off):
noboolalpha
No alphanumerical bool values (function )
noshowbase
Do not show numerical base prefixes (function )
noshowpoint
Do not show decimal point (function )
noshowpos
Do not show positive signs (function )
4

noskipws
Do not skip whitespaces (function )
nounitbuf
Do not force flushes after insertions (function )
nouppercase
Do not generate upper case letters (function )
Numerical base format flags ("basefield" flags):
dec
Use decimal base (function )
hex
Use hexadecimal base (function )
oct
Use octal base (function )
Floating-point format flags ("floatfield" flags):
fixed
Use fixed floating-point notation (function )
scientific
Use scientific floating-point notation (function )
Adustment format flags ("adjustfield" flags):
internal
Adjust field by inserting characters at an internal position (function )
left
Adjust output to the left (function )
right
Adjust output to the right (function )

Input manipulators
ws
Extract whitespaces (function )
Output manipulators
endl
Insert newline and flush (function )
ends
Insert null character (function )
flush
Flush stream buffer (function )
Parameterized manipulators
These functions take parameters when used as manipulators. They require the explicit
inclusion of the header file<iomanip>.

setiosflags
Set format flags (function )
resetiosflags
Reset format flags (function )
setbase
Set basefield flag (function )
setfill
Set fill character (function )
setprecision
Set decimal precision (function )
setw
Set field width (function )
6

4. Reference Variable
A reference variable is an alias, that is, another name for an already existing variable.
Once a reference is initialized with a variable, either the variable name or the reference
name may be used to refer to the variable.
C++ References vs Pointers:
References are often confused with pointers but three major differences between
references and pointers are:

You cannot have NULL references. You must always be able to assume that a
reference is connected to a legitimate piece of storage.

Once a reference is initialized to an object, it cannot be changed to refer to another


object. Pointers can be pointed to another object at any time.

A reference must be initialized when it is created. Pointers can be initialized at any time.
Creating References in C++:
Think of a variable name as a label attached to the variable's location in memory. You
can then think of a reference as a second label attached to that memory location.
Therefore, you can access the contents of the variable through either the original
variable name or the reference. For example, suppose we have the following example:
int

i = 17;

We can declare reference variables for i as follows.


int&

r = i;

Read the & in these declarations as reference. Thus, read the first declaration as "r is
an integer reference initialized to i" and read the second declaration as "s is a double
reference initialized to d.". Following example makes use of references on int and
double:
#include <iostream>
using namespace std;
int main ()
{
// declare simple variables
int i;
double d;
// declare reference variables
int& r = i;
double& s = d;

i = 5;
cout << "Value of i : " << i << endl;
cout << "Value of i reference : " << r << endl;
d = 11.7;
cout << "Value of d : " << d << endl;
cout << "Value of d reference : " << s << endl;
return 0;
}
When the above code is compiled together and executed, it produces the following
result:
Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
References are usually used for function argument lists and function return values. So
following are two important subjects related to C++ references which should be clear to
a C++ programmer:
Concept

Description

References as parameters

C++ supports passing references as function


parameter more safely than parameters.

Reference as return value

You can return reference from a C++ function


like a any other data type can be returned.

Types of errors in C++


1. Syntax error:- This error occurs due to following reason.
I. Not following the grammatical rules used in declaration of identifier.
II. Not declaring an identifier used in program.
III. Not terminating statement by semicolon.
IV. Not providing equal number of opening and closing braces etc.
These errors can be rectified by the user as it is displayed while compiling the program.
2.

3.

Logical error: - This error wont be displayed on the screen. However it will lead to
display wrong results. Example: An infinite loop. This errorlead to abnormal
termination of a program or infinite loop.
Runtime error: - This error occurs while running a program by displaying the
message listed below.
8

I. Division by 0.
II. Overflow
III. Underflow
Now lets have an example:Q. Write a corrected code of the following program segment by underlining the error
corrected.
void main()
{
int a,b;
cin<<a;
b=a;
cout <<"b=",a;
};
Answer:#include<iostream.h>
void main()
{
int a,b;
cin>>a;
b=a;
cout <<"b="<<a;
}

References:
www.en.cppreference.com/w/cpp/language/types
www.cplusplus.com/doc/tutorial
www.cpluspluslearner.blogspot.com/2012/05/types-of-errors-in-c_11.html

You might also like