Data types in C++

A data type is used to indicate the type of data value stored in a variable. All C compilers support a variety of data types. This variety of data types allows the programmer to select the type appropriate to the needs of the application as well as the machine. With the exception of void the basic datatypes may have several modifiers receding them to serve the needs of various situations. The modifiers signed, unsigned, long and short may applied to character and integer basic data types. ANSI C supports the following classes of data types.

1.Primary (fundamental) data types.
2.Derived data types.
3.User-defined data types

basic data types in c++
basic data types in c++

1. Primary Data Types:

TypeSize in BitsDetails
void2void specifies void or null types.
char8Any encoding of 8 bits or less (e.g.ASCII) can be used to store characters.
Integer operations can be performed only for the range 0 ~ 127.
All bits contribute to the value of the char, i.e. there are no “holes” or “padding” bits.
signed char8Characters stored like for type char.
Can store integers in the range -127 to + 128
unsigned char8Characters stored like for type char.
Can store integers in the range 0 to 255.
short
(short int, signed short, signed short int)
16Can store integers in the range -32767 to +32768
Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to using int.
unsigned short
(unsigned short int)
16Can store integers in the range 0 to 65535 portably.
Used to reduce memory usage (although the resulting executable may be larger and probably slower as compared to using int.
int
(signed, signed int)
16Represents the “normal” size of data the processor deals with (the word size); this is the integral data-type used normally.
Can store integers in the range -32767 to 32767 portably.
unsigned int16Can store integers in the range 0 to 65535 portably.
long
(long int, signed long, signed long int)
32Can store integers in the range -2147483647 to 2147483647 portably
unsigned long
(unsigned long int)
32Can store integers in the range 0 to 4294967295 portably.
boolNot SpecifiedCan store the constants true and false.
float32Used to reduce memory usage when the values used do not vary widely.
The floating-point format used is implementation defined and need not be the IEEE single-precision format.
unsigned cannot be specified.
double≥ size of
float
Represents the “normal” size of data the processor deals with; this is the floating-point data-type used normally.
The floating-point format used is implementation defined and need not be the IEEE double-precision format.
unsigned cannot be specified.
long double≥ size of doubleunsigned cannot be specified.
basic data types of c++

2. Derived Data Types:

TypeSize in bitsDetails
Reference
(Type &)
≥ size of char (8)References (unless optimzed out) are usually internally implemented using pointers and hence they do occupy extra space separate from the locations they refer to.
Array
(Type [ ])
≥ integer
X size of type (16 X size type)
The brackets ([]) follow the identifier name in a declaration. In a declaration which also initializes the array (including a
function parameter declaration), the size of the array (the integer) can be omitted. Type [] is not the same as type*. Only under some circumstances one can be converted to the other.
Pointer
(Type *)
≥ size of char (8)0 always represents the null pointer (an address where no data can be placed), irrespective of what bit sequence represents the value of a null pointer. Pointers to different types may have different representations, which means they could also be of different sizes. So they are not convertible to one another. Even in an implementation which guarantees all data pointers to be of the same size, function pointers and data pointers are in general incompatible with each other.
• For functions taking variable number of arguments, the arguments passed must be of appropriate type, so even 0 must be cast to the appropriate type in such function calls.
Function
(Type comma delimited list)
The parentheses ( () ) follow the identifier name in a declaration, e.g. a 2-arg function pointer: int (*fptr) (int arg1, int arg2).
Functions declared without any storage class are extern.
Derived Data Types in C++

3. User Defined Data Types:

TypeSize in BitsDetails
struct or class≥ sum of size of each member• Default access modifier for structs for members and base classes is public.
• For classes the default is private.
• The convention is to use struct only for POD types.
• Said to be a compound type.
union≥ size of the largest member• Default access modifier for for members and base classes is public.
• Said to be a compound type.
enum≥ size of charEnumerations are a distinct type from int. int are not implicitly converted to enums, unlike in C.
Also ++/– cannot be applied to enum’s unless overloaded.
typedefsame as the type being
given a name
• typedef has syntax similar to a storage class like static, register or extern.
template≥ size of char
User Defined Data Types in C++

Related posts