Base types Visual C++

,

Base types Visual C++


 


Answers

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.

  1. Floating point types:

float, double, long double.

  1. Character data types:

char (signed char), unsigned char, wchar_t.

  1. Logical data type:

bool.

  1. Data type “enumeration”:

enum.


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

In C++ base integer data types are following:

short int, unsigned short int, int, unsigned int, long (long int), unsigned long (unsigned long int).

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

2 
-100 
398

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.

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


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“?

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);

– if you need to declare the function, that doesn’t take any parameter (see example).

Example. 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();
...


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


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.