You are the only one who can defeat yourself - - - Introduction to C++ Data Types

Posted by pytrin on Wed, 31 Jul 2019 17:25:41 +0200

C++ Data Type

Various variables are needed to store all kinds of information when programming with programming language. A variable retains the memory location of the value it stores. This means that when you create a variable, you leave some space in memory.

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

Basic built-in types

C++ provides programmers with a rich variety of built-in data types and user-defined data types. The following table lists seven basic C++ data types:

type Keyword
Boolean type bool
Character char
integer int
float float
Double floating point type double
Typeless void
Wide character type

wchar_t

In fact, wchar_t comes from this way:

typedef short int wchar_t;

So wchar_t actually has the same space as short int.

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

  • signed
  • unsigned
  • short
  • long

The following table shows the memory required for each variable type to store values in memory, as well as the maximum and minimum values that this type of variable can store.

Note: Different systems will vary.

type position 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 429467295
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 +/- 3.4e +/- 38 (~7 digits)
double 8 bytes +/- 1.7e +/- 308 (~15 digits)
long double 16 bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character

As can be seen from the table above, the size of variables varies depending on the compiler and the computer used.

The following example will output the sizes of various data types on your computer.

#include<iostream>  
#include<string>  
#include <limits>  
using namespace std;  
  
int main()  
{  
    cout << "type: \t\t" << "************size**************"<< endl;  
    cout << "bool: \t\t" << "Number of bytes occupied:" << sizeof(bool);  
    cout << "\t Maximum:" << (numeric_limits<bool>::max)();  
    cout << "\t\t Minimum value:" << (numeric_limits<bool>::min)() << endl;  
    cout << "char: \t\t" << "Number of bytes occupied:" << sizeof(char);  
    cout << "\t Maximum:" << (numeric_limits<char>::max)();  
    cout << "\t\t Minimum value:" << (numeric_limits<char>::min)() << endl;  
    cout << "signed char: \t" << "Number of bytes occupied:" << sizeof(signed char);  
    cout << "\t Maximum:" << (numeric_limits<signed char>::max)();  
    cout << "\t\t Minimum value:" << (numeric_limits<signed char>::min)() << endl;  
    cout << "unsigned char: \t" << "Number of bytes occupied:" << sizeof(unsigned char);  
    cout << "\t Maximum:" << (numeric_limits<unsigned char>::max)();  
    cout << "\t\t Minimum value:" << (numeric_limits<unsigned char>::min)() << endl;  
    cout << "wchar_t: \t" << "Number of bytes occupied:" << sizeof(wchar_t);  
    cout << "\t Maximum:" << (numeric_limits<wchar_t>::max)();  
    cout << "\t\t Minimum value:" << (numeric_limits<wchar_t>::min)() << endl;  
    cout << "short: \t\t" << "Number of bytes occupied:" << sizeof(short);  
    cout << "\t Maximum:" << (numeric_limits<short>::max)();  
    cout << "\t\t Minimum value:" << (numeric_limits<short>::min)() << endl;  
    cout << "int: \t\t" << "Number of bytes occupied:" << sizeof(int);  
    cout << "\t Maximum:" << (numeric_limits<int>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<int>::min)() << endl;  
    cout << "unsigned: \t" << "Number of bytes occupied:" << sizeof(unsigned);  
    cout << "\t Maximum:" << (numeric_limits<unsigned>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<unsigned>::min)() << endl;  
    cout << "long: \t\t" << "Number of bytes occupied:" << sizeof(long);  
    cout << "\t Maximum:" << (numeric_limits<long>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<long>::min)() << endl;  
    cout << "unsigned long: \t" << "Number of bytes occupied:" << sizeof(unsigned long);  
    cout << "\t Maximum:" << (numeric_limits<unsigned long>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<unsigned long>::min)() << endl;  
    cout << "double: \t" << "Number of bytes occupied:" << sizeof(double);  
    cout << "\t Maximum:" << (numeric_limits<double>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<double>::min)() << endl;  
    cout << "long double: \t" << "Number of bytes occupied:" << sizeof(long double);  
    cout << "\t Maximum:" << (numeric_limits<long double>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<long double>::min)() << endl;  
    cout << "float: \t\t" << "Number of bytes occupied:" << sizeof(float);  
    cout << "\t Maximum:" << (numeric_limits<float>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<float>::min)() << endl;  
    cout << "size_t: \t" << "Number of bytes occupied:" << sizeof(size_t);  
    cout << "\t Maximum:" << (numeric_limits<size_t>::max)();  
    cout << "\t Minimum value:" << (numeric_limits<size_t>::min)() << endl;  
    cout << "string: \t" << "Number of bytes occupied:" << sizeof(string) << endl;  
    // < < " t max:" < < ((numeric_limits < string >: max) () < < " t minimum:"< ((numeric_limits < string >: min) () < < endl);  
    cout << "type: \t\t" << "************size**************"<< endl;  
    return 0;  
}

This example uses endl, which inserts a newline character after each line, and the `operator'is used to pass multiple values to the screen. We also use the sizeof() function to get the size of various data types.

When the above code is compiled and executed, it produces the following results, which vary according to the computer used:

type:         ************size**************
bool:         Number of bytes occupied: 1    Maximum: 1        Minimum value: 0
char:         Number of bytes occupied: 1    Maximum:        Minimum value:?
signed char:     Number of bytes occupied: 1    Maximum:        Minimum value:?
unsigned char:     Number of bytes occupied: 1    Maximum:?        Minimum value:
wchar_t:     Number of bytes occupied: 4    Maximum: 2147483647        Minimum value:-2147483648
short:         Number of bytes occupied: 2    Maximum: 32767        Minimum value:-32768
int:         Number of bytes occupied: 4    Maximum: 2147483647    Minimum value:-2147483648
unsigned:     Number of bytes occupied: 4    Maximum: 4294967295    Minimum value: 0
long:         Number of bytes occupied: 8    Maximum: 9223372036854775807    Minimum value:-9223372036854775808
unsigned long:     Number of bytes occupied: 8    Maximum: 18446744073709551615    Minimum value: 0
double:     Number of bytes occupied: 8    Maximum: 1.79769e+308    Minimum: 2.22507e-308
long double:     Number of bytes: 16    Maximum: 1.18973e+4932    Minimum: 3.3621e-4932
float:         Number of bytes occupied: 4    Maximum: 3.40282e+38    Minimum: 1.17549e-38
size_t:     Number of bytes occupied: 8    Maximum: 18446744073709551615    Minimum value: 0
string:     Number of bytes: 24
type:         ************size**************

typedef statement

You can use typedef to take a new name for an existing type. Here is a new type of grammar defined using typedef:

typedef type newname; 

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

typedef int feet;

Now, the following statement is perfectly legitimate, creating an integer variable distance:

feet distance;

Enumeration type

Enumeration is a derived data type in C++, which is a set of user-defined enumeration constants.

If a variable has only a few possible values, it can be defined as an enumeration type. The so-called "enumeration" refers to enumerating the values of variables one by one, and the values of variables can only be within the range of enumerated values.

To create enumerations, you need to use the keyword enum. The general form of enumeration type is:

enum enumeration{ 
     Identifier [= integer constant], 
     Identifier [= integer constant], 
... 
    Identifier [= integer constant]
} Enumeration of variables;
    

If the enumeration is not initialized, that is, when "= integer constant" is omitted, the first identifier begins.

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

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

By default, the first name has a value of 0, the second name has a value of 1, the third name has a value of 2, and so on. However, you can also assign a special value to the name, just by adding an initial value. For example, in the following enumeration, green has a value of 5.

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

Here, blue has a value of 6, because by default, each name is 1 larger than the previous name, but red still has a value of 0.

Topics: Programming