Test your Programming knowledge

50% users failed to get the best score. It's your turn to test now.
Questions curated by the expert mentors at codzify.com

Start Quiz

Article

Data Types in C++

3 min 30 sec read

When we write any program we create variables and the job of data type is to tell the compiler how much memory a specific variable is going to take to store the values. Datatypes specify the type of data where variables can be stored.

Data types in C++ includes Primitive Data Types like int, char, float, double and User-defined Data Types like Structure, Union, enum. User-defined data types mean which are defined by the user.

  1. Basic Datatypes
  2. Derived Datatypes
  3. Enumeration Datatype
  4. void Datatype
Sr. No Datatypes Datatype Description
1) Basic Data Type int, char, float, double
2) Derived Data Type Array, Pointer, Structure, Union
3) Enumeration Data Type enum
4) void void return nothing.
5) bool Boolean ( True or False )
6) wchar_t Wide character.

Basic Data Types

In Interview aptitude questions you might experience the questions like what is the size of particular data type? Basic Data types in C++ you should know about,

Sr. No Datatype Size
1) char Character Type - 1 byte
2) int Integer Type - 2 or 4 bytes depends on the compiler.
3) float Floating type - 4 bytes
4) double Double type - 8 bytes

typedef Declarations:

typedef is used to give a new name to existing datatypes. In this example we will give the exisiting float datatype some name which we use in our entire Cpp program. For example:

typedef float codzify;

Now you have given the name to float type as codzify. Now you can use it whenever you want to declare the float variable.

//Declare a float variable.
codzify abc = 1.0;

Sample program using typedef :-

#include 
using namespace std;
typedef float codzify;

int main()
{
// Here we used float type with different name.
  codzify a = 1.0;
  cout<

Output:

1.0

Enumerated Types

An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators.

In simple words, enumerated contains a list of identifiers whose first value is initialized with 0 by default. Its next value is being initialised with 1. Then it's next with 2.. then 3 etc.

You can also initialise the enum values as per your need.

Example :-

enum day(mon, tues, wed, thurs, fri) d;

mon = 0 will be by default. tue = 1 will be 1 If you assign tues = 7 then wed = 8 , thurs = 9 , fri = 10.

Derived Data Types

Derived Data types like Arrays, Pointers, Structure, Union are explained in detail in our upcoming articles. We will explore one by one. Just get the basics and have some rough information that these are the datatypes.

 

Recommended Articles

Did you found this article helpful?

Try to execute what you have learnt

Easy to use online data structure compiler where you can execute the programs in your favourite programming language.
(C, C++, Python)

Open Compiler

HTML, CSS and Javascript Real time Web Editor

Execute your HTML, CSS and javascript code in real time with the web editor
(HTML, CSS, Bootstrap, Javascript)

Open Web Editor