C++ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Data Types

C++ Data Types

Data type

shape Description

Every programming language consists of some kind of data. Datatype shows the specific data entered into a program. CPP Data Types are the keywords used to declare the type of the variable used in the program such as integer, real, character and string.

shape Syntax

Below is the syntax for CPP Data Types. The type of data has to be provided in the place of datatype.  
datatype variable_name;

shape Conceptual figure

Three types of data types are available such as:

Built- in Data type

shape Description

Built-in data types in C++ are similar to the built-in data type of C. These are also called as"Fundamental Data Types" or "Primary Data type". Below are the datatypes belonging to Built-in Datatypes.

Integer Data Type

shape Description

Integer Datatype is used to denote integer values i.e they store only the whole numbers without decimal places.
  • The keyword used is "int"
  • Format Specifier for integer data type is %d. (Format specifier is used to display/print integer values using any output functions such as 'printf and print')
1) Integer data types are whole numbers which does not allow the precision i.e decimal places. 2) Occupies 2 bytes of memory. 3) The range lies between "-32768 and +32767".
1) Takes only the positive values. 2) Occupies 2 bytes of memory. 3) The range is double the signed 'int' i.e. "0 to 65535".
1) Needs less memory space. 2) Occupies 1 byte of Memory. 3) short int a; and int a; both are same.
Same as unsigned int which occupies less space.
1) If the memory required is more than "int and short int" then long int is used. 2) Occupies 4 bytes of memory. 3) The range lies between "-2147483648 and + 2147483647".
1) The size is same as signed long int. 2) The range lies between "0 and 42949672954".

shape Example

[c] #include <iostream> using namespace std; int main() { cout << "Size of int is " << sizeof(int) << endl; cout << "Size of short int is " << sizeof(short int) << endl; cout << "Size of long int is " << sizeof(long int) << endl; return 0; } [/c] Output: [c] Size of int : 4 Size of short int : 2 Size of long int : 4 [/c]

Character Data Type

shape Description

Character Data Type is used to denote character value. The keyword used is "char". Format specifier for character data type is %c.
1) Occupies 1 byte of memory. 2) The range lies between "0 and 255"
1) Occupies 1 byte of memory. 2) The range lies between "-128 and 127"

shape Example

[c] #include<iostream> using namespace std; int main() { cout << "Enter a character" << endl; char c; cin >> c; //converting character value to integer value using static cast cout << "The ASCII value of given character is " << static_cast<int>(c) << endl; return 0; }[/c] Output: [c] Enter a character r The ASCII value of given character is 114[/c]

Floating Point Data Type

shape Description

Floating Point Data Type is used to denote the decimal places for integer values. The keyword used is "float".The format specifier for Floating Point Data Type is %f.
1) Occupies four bytes of memory. 2) The range lies between "-3.4E+38 and +3.4E+38".
1) Occupies eight bytes of memory. 2) The range lies between "-1.7E+308 and +1.7E+308". 3) The format specifier for %lf.
1) Occupies range of 10 bytes of memory. 2) The range is same as double. 3) The format specifier is %Lf.

shape Example

[c]#include<iostream> using namespace std; int main() { float s=958637775.45f; cout << s << endl; float f = 3.33333333333333333333333333333333333333f; cout << f << endl; double d = 3.3333333333333333333333333333333333333; cout << d << endl; return 0; }[/c] Output: [c] 9.58698e+008 3.33333 3.33333[/c]

Void Data Type

shape Description

Void type represents no value. Even though void doesn't have any value,it can be used in two ways. 1) For a function that does not return anything. Basically, computations are done within the function and there is nothing to return. Usually, those kind of functions does not appear. Eg:
void func(int i, int j, int *k) { *k = i + j }
2) To have a generic pointer which can point to any address. Eg:
int i = 20 float j = 10 void *i = (void *)&j i = (void *)&j
But void pointers cannot be incremented, decremented or deferenced. It always has to be typecasted to an actual primitive class/ type and then pointer manipulations can be performed. All the concepts are discussed further in detail in Functions and Pointers chapters.

Boolean Data Type

shape Description

Boolean Type denotes either true (or) false when the logic is applied. It uses the keyword "bool".

shape Example

[c] #include<iostream> using namespace std; int main() { bool b1(true); cout<<(b1) <<endl; cout <<(!b1 )<< endl; bool b2(false); cout << (b2 ) << endl; cout << (!b2) << endl; return 0; }[/c] Output: [c] 1 0 0 1[/c]

User-defined Data type

shape Description

The user defines it based on the existing data types.They are formed by the combination of all the requirements of the user. In C++- programming, a variable consists of "Single Data and Single Data Type". An array consists of "Multiple Data and Single Data Type". In addition, structures consists of "Multiple Data and Multiple Data Types".

Structure

shape Description

Structure can be defined as group of variables with same name but different CPP Data Types. These are discussed in Data Structures chapter in detail.

Class

shape Description

Class is the blue-print of an object. A single C++ program can contain any number of classes. They are the user-defined CPP Data Types that consists of objects and is the combination of data and functions.

Enumerated CPP Data Types

shape Description

Enum is a user-defined data type which specifies a type having a group of named integer constants. In other words, all the similar identifiers are named under a single type. Syntax for enum is as follows.
enum enum_name{enumerators_list}enum_type;
Either enum_name or enum_type is given. The compiler does not take both the values. The values in the enumeration list must be separated by comma and the entire enumeration should be ended with a semicolon. One cannot assign their own values to the enumerators because the values increase accordingly. Eg:
enum cards{spades,clubs,diamonds,hearts};
Declaration: When a variable of the enumerated type is defined, then only memory is allocated for that variable at that particular time. Every enumerators are assigned values as per the arrangement of enumeration list. By default, the values starts from 0. The next value is incremented and so on.

shape Example

In the below example,Cards is of enum data type having similar identifiers under Cards. [c] #include <iostream> #include <string> using namespace std; enum Cards { cards_spades, cards_hearts, cards_clubs, cards_diamonds, }; string getCards(Cards cards) { if (cards == cards_spades) return string("spades"); if (cards == cards_hearts) return string("hearts"); if (cards == cards_clubs) return string("clubs"); if (cards == cards_diamonds) return string("diamonds"); } int main() { Cards cards(cards_spades); cout << "You got " << getCards(cards_spades) << "\n"; return 0; }[/c] Output: [c]You got spades[/c]

Union

shape Description

Union is similar to structures. The keyword used is "union". It is used mainly to save the memory space and to store the values dynamically. The differences between union and structure is,
  • Structure allocates memory for every member of it separately. Union allocates a single shared storage space for all its members which is the size of its biggest data member.
  • In Structures, manipulations made on one member does not effect the other. In Union, since only single member is allocated at a time, manipulations done on one member reflects the other.
Syntax will be as follows:
union union_name { //declarations and initialization of members; };
Accessing : The union variable can be retrieved in the same manner as in the structure. To access the union variable . dot operator is used and to get the address of union variable * is used. The union members can be accessed only one at a time. Unions are not much used when compared to structures as they does not store all the variables at a time.[/post_flow_steps_user_def] Declaring : Union can be declared with the keyword union enclosed in {} followed by ;
union example { char c; int a; float b; } ex;
Initializing : The values are initialized with the datatypes inside the braces and the union members attached to the union with . operator.
union example { char c; int a; float b; } ex; ex.c; ex.a; ex.b;

shape Example

[c] #include<iostream> using namespace std; union splessons { double a; int b; char c; }; int main() { splessons numbers; numbers.a = 3.14; numbers.b = 52; numbers.c = 'R'; cout<<numbers.a<<endl; cout<<(numbers.b)<<endl; cout<<numbers.c<<endl; cout<<static_cast<int>(numbers.c)<<endl; return 0; }[/c] Output: [c] 3.14 52 R 82[/c]

Derived Data type

shape Description

Derived Data Types are derived from Primary Data Types. "Arrays, Pointers, References and Functions" are derived data types. These are discussed in further chapters in detail.

Summary

shape Key Points

  • CPP Data Types defines type of data
  • Integer(%i) has "Unsigned, Signed, Short and Long Types"
  • Float(%f) has "Double and Long Double Types"
  • Char(%c) has "Signed and Unsigned types"
  • Void does not return any value
  • All the similar identifiers are named under a single type Enum.