Basic Data Types In C++


1) Integral type : – The data types in this type are int and char. The modifiers signed, unsigned, long & short may be applied to character & integer basic data type. The size of int is 2 bytes and char is 1 byte.

2) void – void is used :
i) To specify the return type of a function when it is not returning any value.
ii) To indicate an empty argument list to a function.
Ex- void function1(void)
iii) In the declaration of generic pointers.
Ex- void *gp

A generic pointer can be assigned a pointer value of any basic data type.
Ex . int *ip // this is int pointer
gp = ip //assign int pointer to void.

A void pointer cannot be directly assigned to their type pointers in c++ we need to use cast operator.

Ex – void *ptr1;
char *ptr2;
ptr2 = ptr1; is allowed in c but not in c++.
ptr2 = (char *)ptr1; is the correct statement.

3) Floating type:
The data types in this are float & double. The size of the float is 4 byte and double is 8
byte. The modifier long can be applied to double & the size of long double is 10 byte.

User-defined type:
i) The user-defined data type structure and union are same as that of C.

ii) Classes – Class is a user defined data type which can be used just like any other
basic data type once declared. The class variables are known as objects.

iii) Enumeration
a) An enumerated data type is another user defined type which provides a way of  attaching names to numbers to            increase simplicity of the code.
b) It uses enum keyword which automatically enumerates a list of words by assigning them values 0, 1, 2,…..etc.
c) Syntax:-
          enum shape {
circle, square, triangle;

Now shape becomes a new type name & we can declare new variables of this type.
Ex . shape oval;
d) In C++, enumerated data type has its own separate type. Therefore c++ does not
permit an int value to be automatically converted to an enum value.
Ex. shape shapes1 = triangle, // is allowed
shape shape1 = 2; // Error in c++
shape shape1 = (shape)2; //ok
e) By default, enumerators are assigned integer values starting with 0, but we can override the default value by               assigning some other value.
Ex enum colour {red, blue, pink = 3}; //it will assign red to o, blue to 1, & pink to 3 or enum colour {red = 5,         blue, green}; //it will assign red to 5, blue to 6 & green to 7.

Derived Data types:
1) Arrays
An array in c++ is similar to that in c, the only difference is the way character arrays are
initialized. In c++, the size should be one larger than the number of character in the
string where in c, it is exact same as the length of string constant.
Ex – char string1[3] = “ab”; // in c++
char string1[2] = “ab”; // in c.

2) Functions
Functions in c++ are different than in c there is lots of modification in functions in c++
due to object orientated concepts in c++.

3) Pointers
Pointers are declared & initialized as in c.
Ex int * ip; // int pointer
ip = &x; //address of x through indirection
c++ adds the concept of constant pointer & pointer to a constant pointer.
char const *p2 = .HELLO.; // constant pointer
Next you have a summary of the basic fundamental data types in C++, as well as the range of values that can be represented with each one:

Name Description Size* Range*
char – 1byte; signed: -128 t127, unsigned: 0 to 255
short int (short)
short Integer – 2bytes; signed: -32768 to 32767, unsigned: 0 to65535
int Integer. 4bytes signed: – 2147483648 to 2147483647, unsigned: 0 to 4294967295
long int or (long)
Long integer. 4bytes signed: -2147483648 to 2147483647, unsigned: 0 to 4294967295
Bool – Boolean value. It can take one of two values: true or false.
1byte true or false
Float Floating point number.- 4bytes 3.4e +/- 38 (7 digits)
double Double precision
floating point number.