C++ Data Types

C++ Data Types
Techiio-author
Written by Sagar RabidasJanuary 25, 2022
9 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss C++ Data Types

C++ Data Types

In this newsletter, we will have a look at one-of-a-kind data sorts which include integer, go with the flow, double, char, or integrated records types like union, enum, struct, and facts kinds like features, recommendations, arrays. Information types need to be defined earlier than the execution because it informs the compiler of the sort of facts unique variables holds. The integer statistics kind can maintain the handiest integer values, it cannot maintain the go with the flow values or string values.

A records type is to allow recognizing the variable, what kind of detail it's far, and going to determine the reminiscence allocation of that variable. We're aware that the kind of each record has a special reminiscence allocation. There are three distinctive c++ records kinds namely; primitive, derived, and a person described.

Top 3 Data Types in C++

Here are three different data types in C++ which are given below:

  • Primitive Data Types

These are pre-defined in c++, also called the built-in data types. We can directly use them to declare the variables.

Integer: Usually defined by “int”. We can know the size of memory allocated and how the variable is declared as below.

Code:

#include <iostream>
using namespace std;
int main()
{
int a;
cout<< " Size of int is: " << sizeof(a);
}

Character: Usually defined by “char”. We can know the size of memory allocated and how the variable is declared as below.

Code:

#include <iostream>
using namespace std;
int main()
{
char a;
a='R';
cout<< " Size of char is: " << sizeof(a)<<endl;
cout<< " Value of a is: " << a;
}

Floating Point: Usually defined by “float”. We can know the size of memory allocated and how the variable is declared as below.

Code:

#include <iostream>
using namespace std;
int main()
{
float a;
a=5.85;
cout<< " Size of float is: " << sizeof(a)<<endl;
cout<< " Value of a is: " << a;
}

Boolean: Usually defined by “bool”. We can know the size of memory allocated and how the variable is declared as below.

Code:

#include <iostream>
using namespace std;
int main()
{
bool a;
cout<< " Size of bool is: " << sizeof(a)<<endl;
cout<< " Value of a is: " << a;
}

String: Usually defined by “String”. We can know the size of memory allocated and how the variable is declared as below.

Code:

#include <iostream>
using namespace std;
int main()
{
string a;
a="Happy";
cout<< " Size of string is: " << sizeof(a)<<endl;
cout<< " Value of a is: " << a;
}

Derived Data Types

These are the data types that are derived from the primitive data types; which in turn justifies its name.

Array: Here, we define a series. Let’s see how we can do that here.

Code:

#include <iostream>
using namespace std;
int main()
{
int a[5]={1,6,10,15,56};
cout<< " Size of array is: " << sizeof(a)<<endl;
for(int i=0;i<6;i++)
{
cout<< " Value of a is: " << a[i] <<endl;
}
}

Pointer: This enables the call-by-reference functionality and these pointers play a huge role in declaring or manipulating data in dynamic data structures. For example in creating Stacks, Queues, Linked lists we primarily use these pointers.

Code:

#include <iostream>
using namespace std;
int main()
{
float a = 30;
float *h;
h= &a;
cout << " Value of pointer h "<< h << endl;
cout << " Value of variable a "<< a << endl;
cout << " h value "<< *h ;
}
  • User-Defined Data Types

As the name already suggests, these are the data types that the user can define. Let us see a few examples of these.

Structures: Storing the combination of either similar or different data types under continuous memory locations. As we already saw, in arrays we can store only items with similar data types. But structures can store different data types.

Code:

#include <iostream>
using namespace std;
struct First
{
int a = 58;
string r = "Happy";
float y = 58.5;
} ;
int main()
{
struct First f;
cout<< " Integer value is: "<< f.a <<endl;
cout<< " String value is: "<< f.r << endl;
cout<< " Float value is: "<< f.y;
}

Class: It is defined in object-oriented programming. This has functions, variables and is accessed by creating objects.

Code:

#include <iostream>
using namespace std;
class First
{
public:
string name;
void show()
{
cout << "Name is: " << name;
}
};
int main()
{
First f;
f.name = "My Name";
f.show();
return 0;
}

TypeDef: This data type is for just giving a new or a different name to the data types.

Code:

#include <iostream>
using namespace std;
int main()
{
typedef unsigned char THISONE;
typedef unsigned int OTHERONE;
THISONE b1;
OTHERONE b2;
b1 = 'R';
b2 = 10;
cout << " Check this out: " << b1<<endl;
cout << " Check other out: " << b2;
return 0;
}

Enumeration: Defined by the word “enum”. These are generally used when we already know a set of values for a particular variable and choose a single value from them.

Code:

#include <iostream>
using namespace std;
enum color {Yellow, Red, Green, Blue}col;
int main()
{
col = Green;
cout<<" The color chosen is in the place: "<<col;
return 0;
}

c
c plus plus
CPlusPlusDataTypes
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance