Data types
are means to identify the type of data and associated operations of handling
it. There are three types of data types:
- Pre-defined
DataTypes
- Derived Data
Types
- User-defined
DataTypes
Pre-defined DataTypes:
These data types are built-in or predefined data types and can be
used directly by the user to declare variables. example: int, char , float,
bool etc. Primitive data types available in C++ are:
1.
Integer
2.
Character
3.
Boolean
4.
Floating Point
5.
Double Floating Point
6.
Valueless or Void
7.
Wide Character
Derived Data Types:
The data-types that are derived from the primitive or built-in datatypes
are referred to as Derived Data Types. These can be of four types namely:
1.
Function
2.
Array
3.
Pointer
4.
Reference
Abstract or User-Defined Data Types:
These data types are defined by user itself. Like,
defining a class in C++ or a structure. C++ provides the following user-defined
datatypes:
1.
Class
2.
Structure
3.
Union
4.
Enumeration
5.
Typedef defined DataType
In this article,
the Pre-defined DataType is explained:
There are
five data types in the C subset: character, integer, floating-point,
double floating-point, and
valueless (char, int, float, double, and void,
respectively).
To
the five basic data types defined by C, C++ adds two more: bool and
wchar_t.
The exact
format of floating-point values will depend upon how they are implemented.
Integers will
generally correspond to the natural size of a word on the host computer.
Values of
type char are generally used to hold values defined by the ASCII
character
set. Values
outside that range may be handled differently by different compilers.
The range of float
and double will depend upon the method used to represent
the floating-point numbers.
Whatever the method, the range is quite large.
The typical sizes are mentioned
below
Or you can run this code:
#include<iostream>
using namespace std;
int main()
{
cout << "Size of char : " << sizeof(char)
<< " byte" << endl;
cout << "Size of int : " << sizeof(int)
<< " bytes" <<
endl;
cout << "Size of short int : " << sizeof(short
int)
<< " bytes" <<
endl;
cout << "Size of long int : " << sizeof(long
int)
<< " bytes" <<
endl;
cout << "Size of signed long int : " <<
sizeof(signed long int)
<< " bytes" <<
endl;
cout << "Size of unsigned long int : " <<
sizeof(unsigned long int)
<< " bytes" <<
endl;
cout << "Size of float : " << sizeof(float)
<< " bytes"
<<endl;
cout << "Size of double : " << sizeof(double)
<< " bytes" << endl;
cout << "Size of wchar_t : " <<
sizeof(wchar_t)
<< " bytes"
<<endl;
}
Like this
Then you will see an output like this
DataTypes and
their forms
1.
Integer:
Keyword used for integer data types is int.
2.
Character:
Character data type is used for storing characters. Keyword used for character
data type is char.
3.
Boolean:
Boolean data type is used for storing boolean or logical values. A boolean
variable can store either true or false. Keyword
used for boolean data type is bool.
4.
Floating Point:
Floating Point data type is used for storing single precision floating point
values or decimal values. Keyword used for floating point data type is float.
5.
Double Floating Point:
Double Floating Point data type is used for storing double precision floating
point values or decimal values. Keyword used for double floating point data
type is double..
6.
void:
Void means without any value. void datatype represents a valueless entity. Void
data type is used for those function which does not returns a value.
7.
Wide
Character: Wide character data type is also a character data type Represented
by wchar_t.
Modifying the
Basic Types
Except for
type void, the basic data types may have various modifiers preceding
them.
You use a modifier
to alter the meaning of the base type to fit various situations more
precisely.
The list of modifiers is shown here:
signed
unsigned
long
short
You can apply
the modifiers signed, short, long, and unsigned to
integer base types.
You can apply unsigned and signed
to characters. You may also apply long to double.
The use of signed
on integers is allowed, but redundant because the default integer
declaration
assumes a signed number. The most important use of signed is to modify
char
in implementations in which char is unsigned by default.
The
difference between signed and unsigned integers is in the way that the
high order
bit of the
integer is interpreted. If you specify a signed integer, the compiler
generates
code that assumes that the high-order bit of an integer is to be used as a
sign
flag. If the sign flag is 0, the number is positive; if it is 1, the
number is negative.
In general,
negative numbers are represented using the two's complement approach,
which
reverses all bits in the number (except the sign flag), adds 1 to this number,
and
sets the sign
flag to 1.
Signed
integers are important for a great many algorithms, but they only have half
the absolute
magnitude of their unsigned relatives. For example, here is 32,767:
0 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
If the
high-order bit were set to 1, the number would be interpreted as −1. However,
if you
declare this to be an unsigned int, the number becomes 65,535 when the
high order
bit is set to 1.
check the values at that website mentioned above :
-32767 1000000000000001
-65535 10000000000000001
32767 0111111111111111
65535 01111111111111111
Hope that helps, its two’s compliment method for binary conversion
Declaration
of variables
C++ is a strongly-typed language, and requires every variable to
be declared with its type before its first use. This informs the compiler the
size to reserve in memory for the variable and how to interpret its value.: we
simply write the type followed by the variable name . For example:
1.
int a;
2.
float mynum;
The first one declares a variable of type int with the identifier
a. The second one declares a variable of type float with the identifier mynum.
Once declared, the variables a and mynum can be used within the rest of their
scope in the program.
If declaring more than one variable of the same type, they can all
be declared in a single statement by separating their identifiers with commas.
For example:
int a, b ;
This declares three variables (a, b), all of them of type int, and
has exactly the same meaning as:
1 int a;
2 int b;
if you run this code
String
Constants
C/C++
supports one other type of constant: the string. A string is a set of characters
enclosed in
double quotes. For example, "this is Science Geek" is a string.
. Although C allows
you to define string constants, it does not formally have a string data type.
(C++ does define a string class,
however.)
You must not
confuse strings with characters. A single character constant is enclosed in
single quotes, as in 'a'. However,
"a" is a string containing only one letter. in single quotes, as
in 'a'. However, "a" is a string containing only one letter
if you run this the result will be like this
Backslash
Character Constants
Enclosing
character constants in single quotes works for most printing characters. A
few, however,
such as the carriage return, are impossible to enter into a string from the
keyboard. For this reason, C/C++
include the special backslash character constants shown
THANK YOU !! PLEASE DO SHARE THIS POST
Share on Whatsapp