Академический Документы
Профессиональный Документы
Культура Документы
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
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:
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.
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.
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.
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;
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
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