Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The data type of the C+


May 11, 2021 C++


Table of contents


The type of data for C+

When programming in a programming language, you need to use a variety of variables to store a variety of information. T he variable retains the memory location of the value it stores. This means that when you create a variable, some space is reserved in memory.

You may need to store information about various data types (such as character type, wide character type, integer, floating-point type, double floating-point type, Boolean type, etc.), and the operating system allocates memory and decides what to store in reserved memory based on the data type of the variable.

Basic built-in type

It provides programmers with a wide variety of built-in data types and user-defined data types. The following table lists seven basic types of data for C?

type Keyword
Boolean bool
Character pattern char
Integrity int
Floating point float
Dual floating point double
Uncustomary void
Wide character type wchar_t

Some basic types can be decorated with one or more type modifiers:

  • signed
  • unsigned
  • short
  • long

The following table shows the memory that various variable types need to consume to store values in memory, as well as the maximum and minimum values that variables of that type can store.

Note: Different systems may vary.

Type bit Range
char 1 byte -128 to 127 or 0 to 255
unsigned char 1 byte 0 to 255
signed char 1 byte -128 to 127
Int 4 bytes -2147483648 to 2147483647
unsigned int 4 bytes 0 to 4294967295
signed int 4 bytes -2147483648 to 2147483647
short int 2 bytes -32768 to 32767
unsigned short int 2 bytes 0 to 65,535
signed short int 2 bytes -32768 to 32767
long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
signed long int 8 bytes -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
unsigned long int 8 bytes 0 to 18,446,744,073,709,551,615
float 4 bytes s/- 3.4e s/- 38 (-7 digits)
double 8 bytes s/- 1.7e s/- 308 (-15 digits)
long double 16 bytes s/- 1.7e s/- 308 (-15 digits)
wchar_t 2 or 4 bytes 1 wide character

As you can see from the table above, the size of the variables will vary depending on the compiler and the computer you are using.

The following example outputs the size of the various data types on your computer.

#include <iostream>
using namespace std;

int main()
{
   cout << "Size of char : " << sizeof(char) << endl;
   cout << "Size of int : " << sizeof(int) << endl;
   cout << "Size of short int : " << sizeof(short int) << endl;
   cout << "Size of long int : " << sizeof(long int) << endl;
   cout << "Size of float : " << sizeof(float) << endl;
   cout << "Size of double : " << sizeof(double) << endl;
   cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;
   return 0;
}

This example uses endl, which inserts a line break after each line, which is used to pass multiple values to the screen. We also use the sizeof() function to get the sizes of various data types.

When the above code is compiled and executed, it produces the following results, which vary depending on the computer you are using:

Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4

Typedef declaration

You can use typedef to get a new name for an existing type. Here's how to define a new type of syntax using typedef:

typedef type newname; 

For example, the following statement tells the compiler that feet is another name for int:

typedef int feet;

Now that the following declaration is perfectly legal, it creates an integer variable, distance:

feet distance;

The enumerity type

The enumeration type declares an optional type name and a set of identifiers that are used as values for that type. I t has zero or more identifiers that can be used as values for that type. Each enumeral is a constant of enumeral types.

To create an enumerate, you need to use the keyword enum. The general form of enumeration types is:

enum enum-name { list of names } var-list; 

Here, enum-name is the name of the enum-name enumerus type. The list of names, list of names, is separated by a comma.

For example, the following code defines a color enumerity, and the type of variable c is color. Finally, c is assigned "blue".

enum color { red, green, blue } c;
c = blue;

By default, the value of the first name is 0, the value of the second name is 1, the value of the third name is 2, and so on. H owever, you can also give the name a special value, just add an initial value. For example, in the enumeration below, the value of green is 5.

enum color { red, green=5, blue };

Here, the value of blue is 6, because by default, each name is 1 bigger than the one before it.