C++. Base types




Base types C++


  1. Which base (main) types are in C++?
  2. What are the features of using of integer data types?
  3. How to define a variable named x of integer type?
  4. How to set the value into the variable of integer type?
  5. What are the features of floating point data types?
  6. How to define the variable of floating point type?
  7. How set the number values into the floating point variable?
  8. How to convert the variable of type float into variable of type int?
  9. How to convert the variable from type int to type double?
  10. What are the features of using of data of character type?
  11. What features of using the data of bool type?
  12. How calculate the size of any type in C++?
  13. How is realized the initialization of different types variables?
  14. How determine the maximum (minimum) value of variable of specified type?
  15. What are the features of usint the type enum?
  16. What are the features of using the type void? Pointer to void
  17. Can you declare the variable of void type in the program?
  18. What features of using of type wchar_t in Visual C++?
  19. How much of memory is reserved for the declaration of a variable of type wchar_t?
  20. How to determine what maximum (minimum) value a variable of integer or character type can contain? Constants INT32_MIN, INT32_MAX and others

Search other websites:

1. Which base (main) types are in Visual C++?

Answer:
  • 1. Integer data types:
    • short int, unsigned short int, int, unsigned int, long, unsigned long, long long, unsigned long long
  • 2. Floating point types:
    • float, double, long double
  • 3. Character data types:
    • char (signed char), unsigned char, wchar_t
  • 4. Logical data type:
    • bool
  • 5. Data type “enumeration”:
    • enum


2. What are the features of using of integer data types?

In C++, the following basic integer data types are defined:

short int
unsigned short int
int
unsigned int
long (long int)
unsigned long (unsigned long int)
long long
unsigned long long

These data types represent the values of set of integer numbers. For example:

2 
-100 
398
238209802938

The types of data that begin from “unsigned” prefix may contain only positive numbers.

Data types of short int, unsigned short int take two times less space in memory than the data types int, unsigned int. In recent versions of the compiler, data of type short int occupy 2 bytes.

Data of type long, unsigned long take twice a much space in memory than the data of types int, unsigned int.

In earlier versions of compilers, data of type long, unsigned long takes up 2 times more memory space than data of type int, unsigned int. In recent versions of compilers, data of type int (unsigned int) occupy the same place in main memory as data of type long (unsigned long).

Data of type long long (unsigned long long) takes up 2 times more memory space than data of type long (unsigned long).

Example. The example determines the size of the data of the integer type using the sizeof statement. You can read more about the sizeof operator here.

#include <iostream>
using namespace std;

void main()
{
  // The size in bytes of various integer types
  cout << "sizeof(short int) = " << sizeof(short int) << endl;
  cout << "sizeof(unsigned short int) = " << sizeof(unsigned short int) << endl;
  cout << "sizeof(int) = " << sizeof(int) << endl;
  cout << "sizeof(unsigned int) = " << sizeof(unsigned int) << endl;
  cout << "sizeof(long) = " << sizeof(long) << endl;
  cout << "sizeof(unsigned long) = " << sizeof(unsigned long) << endl;
  cout << "sizeof(long long) = " << sizeof(long long) << endl;
  cout << "sizeof(unsigned long long) = " << sizeof(unsigned long long) << endl;
}

The result of the program:

sizeof(short int) = 2
sizeof(unsigned short int) = 2
sizeof(int) = 4
sizeof(unsigned int) = 4
sizeof(long) = 4
sizeof(unsigned long) = 4
sizeof(long long) = 8
sizeof(unsigned long long) = 8

After analyzing the result, we can conclude that in early versions of compilers:

  • variables of types short int and unsigned short int occupy 2 bytes;
  • variables of types int, unsigned int, long (long int), unsigned long (unsigned long int) occupy 4 bytes;
  • variables of types long long (unsigned long long) occupy 8 bytes.


3. How to define a variable named x of integer type?

Answer:
int x; // signed integer

As a result, will be allocated memory space of 4 bytes for variable x. The size of the memory that is allocated for the variable depends on the characteristics of the computer, operating system type, and compiler settings.


4. How to set the value into the variable of integer type?

To do this is used the assignment operator, which is marked by character “=“.

Answer 1. Assignment of value after definition of variable.

int x;
x = 239;

Answer 2. Setting the value in variable during it’s definition (initialization).

int x = 239;


5. What are the features of floating point data types?

Floating point types allow to represent the values from the set of real numbers. For example:

8.35
-990.399
239.0

There are following base floating point types in C++float, double, long double.

Variable of type double take twice a much space in memory than variable of type float. Also, the variable of type “long double” take twice much space in memory than variable of type double.







6. How to define the variable of floating point type?

An example of definition of variables of type float, double, long double:

float f;
double d;
long double ld;


7. How set the number values into the floating point variable?

An example of setting the numeric data in the floating point variables:

float f = -9928.45; // initialization
double d;
long double ld;
d = 0.445332; // assignment operator
ld = 3892923898239.030903; // assignment operator


8. How to convert the variable of type “float” into variable of type “int“?

To do this, is used the operation of type cast. In the brackets you need to assign the type name to which type cast is realized.

Example:

float a;
int b;
a = 8.457;
b = (int) a; // b = 8

When using the type cast operations, it is necessary to take into account the constraints that are imposed on the types that take up less space in the computer’s memory.

For example, variable of type “short int” can represent a smaller range of numbers, than variables of types “float” and “double“. In the next listing occurs the overflow of value in the variable of type “short int“:

short int i;
float f;
f = 3990099.8;
i = (int)f; // i = -7597 - overflow


9. How to convert the variable from type int to type double?

An example of converting from int to double:

int i;
double d;
i = 982;
d = (double)i; // d = 982.0


10. What are the features of using of data of character type?

Data of type char represent the value of character code, which was entered from keyboard. Character code is an integer number.

For example, the code of character ‘f’ is equal to value 102.

The code snippet, where character code is calculated:

int code;
char symbol;
symbol = 'f';
code = (int)symbol; // code = 102

Data of type char are the same integer numbers. Data of type chare occupy one byte in the memory. Characters with codes from 0 to 127 – reserved by BIOS. These include the most frequently used symbols, symbols of numbers, characters of the Latin alphabet. These characters can not be changed.

Characters with codes from 128 to 255 are the regional symbols that linked to a specific alphabet of the computer on which installed the Windows operating system.


11. What are the features of using the data of “bool” type?

Variables of type “bool” can assume only two values: true, false.

These variables are used for checking a logical expressions.

The value of “true” is equal 1. The value of “false” is equal 0.

The code snippet, that determines the number values of “true” and “false“:

int result;
bool b;
result = (int)true; // result = 1
b = false;
result = (int)b;    // result = 0

The code snippet, which converts types “int” and “float” into “bool“:

int i;
float f;
bool b;
i = 6;
b = (bool)i; // b = True
f = 0.0;
b = (bool)f; // b = False


12. How calculate the size of any type in C++?

For this is used operation sizeof().

A code snippet, which determines the size of some data types:

int d;
d = sizeof(char);         // d = 1
d = sizeof(unsigned int); // d = 4
d = sizeof(float);        // d = 4
d = sizeof(double);       // d = 8


13. How is realized the initialization of different types variables?

 

int d = 28;
float z = (float)2.85;
char c = 'k';
String s = "Hello!";
double r = -8.559;


14. How determine the maximum (minimum) value of variable of specified type?

To determine maximum (minimum) value of variable of some type in the .NET Framework are used properties MaxValue and MinValue.

An examples of determining the limit values of variables of different types.

For variables of type “int“:

// int type
int i;
long MaxInt;
long MinInt;
MaxInt = (long)i.MaxValue; // MaxInt = 2147483647
MinInt = (long)i.MinValue; // MinInt = -2147483648

 

For variables of type “short int“:

// type short int
short int si;
int MaxInt;
int MinInt;
MaxInt = (int)si.MaxValue; // MaxInt = 32767
MinInt = (int)si.MinValue; // MinInt = -32768

 

For variables of type unsigned int:

// type unsigned int
unsigned int ui;
unsigned int MaxInt;
unsigned int MinInt;
MaxInt = ui.MaxValue; // MaxInt = 4294967295
MinInt = ui.MinValue; // MinInt = 0

 

For variables of type float:

// type float
float f;
float MaxF;
float MinF;
MaxF = f.MaxValue; // MaxF = 3.402823E+38
MinF = f.MinValue; // MinF = -3.402823E+38

 

For variables of type double:

// type double
double d;
double MaxD;
double MinD;
Max = d.MaxValue; // Max = 1.79769313486232E+308
Min = d.MinValue; // Min = -1.79769313486232E+308

 

For variables of type char:

// type char
char c;
int MaxC;
int MinC;
Max = (int)c.MaxValue; // Max = 127
Min = (int)c.MinValue; // Min = -128


15. What are the features of usint the type “enum“?

Type “enum” – this is a enumeration data type. It sets the mnemonic values for sets of integer. Every mnemonic value has a certain content and is an integer number.

An example of “enum” type for using to denote the months of the year:

enum months { January, February, March, April, May, June, 
              July, August, September, October, November, December } mn;
mn = January;   // mn = 0
mn = March;     // mn = 2
mn = September; // mn = 8

In the above example is described the variable named “mn” of type “enum months“. The mnemonic values of months (January, February, …) are begun from 0 (0, 1, 2, …). To mnemonic value January is corresponding the integer value 0, to mnemonic value February is corresponding the integer value 1 and so on.

So, with the help of an enum type, you can use the mnemonic for better visibility of the source code in the program text.

You can write also:

mn = (enum months)2;  // mn = March
mn = (enum months)11; // mn = December


16. What are the features of using the type “void“? Pointer to void

The “void” data type is used in the following cases:

  • if you need to declare the function, that doesn’t return any value (see example 1);
  • if you need to declare the function, that doesn’t take any parameter (see example 1);
  • when you need to declare a pointer to void, which can interact with pointers to other types (see example 2). In this case, a pointer to void can be cast to a pointer to any other type (int*, float*, double*, etc.).

Example 1. The MyFun() function without parameters, and that doesn’t return any value (it returns the “void” type).

public: void MyFun(void)
{
   // function body
   // ...

   return; // the return from a function, that does not return a value
}

// call the function from program
...
MyFun();
...

Example 2. The example demonstrates some of the features of using a pointer to void type.

#include <iostream>
using namespace std;

void main()
{
  // Declare a pointer to void
  void *pVoid;

  // Declare pointers to other types
  int *pInt = new int;
  float *pFloat = new float;
  char *pChar = new char;
  double *pDouble = new double;

  // Fill with values of pointers to other types
  *pInt = 25;
  *pFloat = (float)3.88;
  *pChar = '+';
  *pDouble = 17.25;

  // Assign pointer to void pointer values to other types
  // - pointer to float type
  pVoid = pFloat; // pointer pVoid points to pFloat memory area
  *(float*)pVoid = 85.77; // change the value *pFloat
  cout << "*pFloat = " << *pFloat << endl; // *pFloat = 85.77

  // - pointer of type char*
  pVoid = pChar; 
  *(char*)pVoid = 'a';
  cout << "*pChar = " << *pChar << endl; // *pChar = a

  // ---------------------------------------------------------------
  // Use a void pointer to exchange int pointer values
  int *p1 = new int; 
  int *p2 = new int;

  // fill in the values of the memory pointed to by pointers p1, p2
  *p1 = 28;
  *p2 = 35;

  cout << "*p1 = " << *p1 << endl; // *p1 = 28
  cout << "*p2 = " << *p2 << endl; // *p2 = 35

  // exchange values pointers through a pointer to void
  pVoid = p1;
  p1 = p2;
  p2 = (int*)pVoid;

  cout << "*p1 = " << *p1 << endl; // *p1 = 35
  cout << "*p2 = " << *p2 << endl; // *p2 = 28 
}

The result of the program

*pFloat = 85.77
*pChar = a
*p1 = 28
*p2 = 35
*p1 = 35
*p2 = 28


17. Can you declare the variable of “void” type in the program?

It is impossible, because the void type is not associated with the value.

The declaration of variable of “void” type leads to a compilation error with message:

Illegal use of type void

Однако можно объявить указатель на void.


18. What features of using of type wchar_t in Visual C++?

The variables of “char” type (see the previous paragraphs) are used for saving 8-bit ASCII-symbol values.

The wchar_t type is used for saving the characters, that are a part of large character set. For example, in the Chinese alphabet has a huge number of characters. 8 bits is not enough to provide the full set of characters of the Chinese alphabet. Therefore, if you want to use the program on the international market, it is advisable to replace a char to wchar_t.

Example of using the wchar_t type.

...

wchar_t t; // for variable t is allocated 2 bytes of memory
t = 's';

...


19. How much of memory is reserved for the declaration of a variable of type wchar_t?

One variable of type wchar_t takes 2 bytes (16 bits) of memory. The range of integer values of type wchar_t variable ranges from 0 to 65535.

20. How to determine what maximum (minimum) value a variable of integer or character type can contain? Constants INT32_MIN, INT32_MAX and others

The values that a variable of integer or character type can receive are determined by the number of bits that the compiler determines to represent this type in memory.

For example, if an integer type variable occupies 2 bytes (16 bits), then the range of valid values may be as follows:

  • for signed variables: -32768…32767;
  • for unsigned variables (with the unsigned prefix): 0…65535.

In C++, there are tools for determining the minimum and maximum values that a variable of integer or character type can take. The C++ library includes the <limits.h> module, which contains a description of the corresponding constants. These constants can be used in programs when intercepting various exceptional situations, for example, overflow, out of range the index in the array, etc.

For example, to compare the value of a variable of type int to the maximum, you can use the constant INT32_MAX for the following example

...
// Determining when an overflow occurs
int i = 0;
while (1)
{
  // increase the counter
  i++;

  // some operations
  // ...

  // compare to the maximum value
  if (i == INT32_MAX)
  {
    cout << "Error. Overflow.";
    return;
  }
}
...

Example. The following example demonstrates the use of constraint constants for character and integer types.

#include <iostream>
using namespace std;

void main()
{
  // Character data types - maximum and minimum values
  cout << "SCHAR_MIN = " << SCHAR_MIN << endl; // char - minimum value
  cout << "SCHAR_MAX = " << SCHAR_MAX << endl; // char - maximum value
  cout << "USCHAR_MAX = " << UCHAR_MAX << endl; // unsigned char - maximum

  // Integer data types - maximum and minimum values
  cout << "SHRT_MIN = " << SHRT_MIN << endl; // short int - minimum value
  cout << "SHRT_MAX = " << SHRT_MAX << endl; // short int - maximum value
  cout << "USHRT_MAX = " << USHRT_MAX << endl; // unsigned short int - maximum
  cout << "INT16_MAX = " << INT16_MAX << endl; // int (16 bit) - minimum
  cout << "INT16_MIN = " << INT16_MIN << endl; // int (16 bit) - maximum
  cout << "UINT16_MAX = " << UINT16_MAX << endl; // unsigned int - maximum 
  cout << "INT32_MIN = " << INT32_MIN << endl; // int (32-bit) - minimum
  cout << "INT32_MAX = " << INT32_MAX << endl; // int (32-bit) - maximum
  cout << "UINT32_MAX = " << UINT32_MAX << endl; // unsigned int - maximum
  cout << "LONG_MIN = " << LONG_MIN << endl; // long int - minimum
  cout << "LONG_MAX = " << LONG_MAX << endl; // long int - maximum
  cout << "ULONG_MAX = " << ULONG_MAX << endl; // unsigned long - maximum
  cout << "LLONG_MAX = " << LLONG_MAX << endl; // long long - maximum
  cout << "LLONG_MIN = " << LLONG_MIN << endl; // long long - minimum
  cout << "ULLONG_MAX = " << ULLONG_MAX << endl; // unsigned long long - maximum
}

The result of the program

SCHAR_MIN = -128
SCHAR_MAX = 127
USCHAR_MAX = 255
SHRT_MIN = -32768
SHRT_MAX = 32767
USHRT_MAX = 65535
INT16_MAX = 32767
INT16_MIN = -32768
UINT16_MAX = 65535
INT32_MIN = -2147483648
INT32_MAX = 2147483647
UINT32_MAX = 4294967295
LONG_MIN = -2147483648
LONG_MAX = 2147483647
ULONG_MAX = 4294967295
LLONG_MAX = 9223372036854775807
LLONG_MIN = -9223372036854775808
ULLONG_MAX = 18446744073709551615


Related topics