Data type for x y. Data types and variables. Data types in the C language for the AVR-GCC compiler

Last update: 11/13/2017

Like many programming languages, C# has its own data type system that is used to create variables. A data type defines the internal representation of the data, the set of values ​​that an object can take, and the valid actions that can be taken on the object.

The C# language has the following primitive data types:

    bool : stores the value true or false (boolean literals). Represented by the system type System.Boolean

    Bool alive = true; bool isDead = false;

    byte: stores an integer from 0 to 255 and occupies 1 byte. Represented by the system type System.Byte

    Byte bit1 = 1; byte bit2 = 102;

    sbyte: stores an integer from -128 to 127 and occupies 1 byte. Represented by the system type System.SByte

    Sbyte bit1 = -101; sbyte bit2 = 102;

    short : stores an integer from -32768 to 32767 and takes 2 bytes. Represented by the system type System.Int16

    Short n1 = 1; short n2 = 102;

    ushort: stores an integer from 0 to 65535 and takes 2 bytes. Represented by the system type System.UInt16

    Ushort n1 = 1; ushort n2 = 102;

    int: stores an integer from -2147483648 to 2147483647 and occupies 4 bytes. Represented by the System.Int32 system type. All integer literals represent int values ​​by default:

    Int a = 10; int b = 0b101; // binary form b =5 int c = 0xFF; // hexadecimal form c = 255

    uint: stores an integer from 0 to 4294967295 and occupies 4 bytes. Represented by the system type System.UInt32

    Uint a = 10; uint b = 0b101; uint c = 0xFF;

    long : stores an integer from –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 and occupies 8 bytes. Represented by the system type System.Int64

    Long a = -10; long b = 0b101; long c = 0xFF;

    ulong: stores an integer from 0 to 18,446,744,073,709,551,615 and takes up 8 bytes. Represented by the system type System.UInt64

    Ulong a = 10; ulong b = 0b101; ulong c = 0xFF;

    float : stores a floating point number from -3.4*10 38 to 3.4*10 38 and occupies 4 bytes. Represented by the system type System.Single

    double : stores a floating point number from ±5.0*10 -324 to ±1.7*10 308 and occupies 8 bytes. Represented by the system type System.Double

    decimal: stores a decimal fraction number. If used without a decimal point, it has a value from ±1.0*10 -28 to ±7.9228*10 28, can store 28 decimal places and occupies 16 bytes. Represented by the system type System.Decimal

    char: stores a single character in Unicode and occupies 2 bytes. Represented by the System.Char system type. Character literals correspond to this type:

    Char a = "A"; char b = "\x5A"; char c = "\u0420";

    string : stores a set of Unicode characters. Represented by the System.String system type. Character literals correspond to this type.

    String hello = "Hello"; string word = "world";

    object : Can store a value of any data type and occupies 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform. Represented by the System.Object system type, which is the base type for all other .NET types and classes.

    Object a = 22; object b = 3.14; object c = "hello code";

For example, let's define several variables different types and print their values ​​to the console:

Using System; namespace HelloApp ( class Program ( static void Main(string args) ( string name = "Tom"; int age = 33; bool isEmployed = false; double weight = 78.65; Console.WriteLine($"Name: (name)"); Console.WriteLine($"Age: (age)"); Console.WriteLine($"Weight: (weight)"); Console.WriteLine($"Working: (isEmployed)" ) )

To output data to the console, interpolation is used here: a $ sign is placed in front of the line and after that we can enter variable values ​​into the line in curly brackets. Console output of the program:

Name: Tom Age: 33 Weight: 78.65 Works: False

Using suffixes

When assigning values, keep in mind the following subtlety: all real literals are treated as values ​​of type double . And to indicate that a fractional number represents a float type or a decimal type, you need to add a suffix to the literal: F/f for float and M/m for decimal.

Similarly, all integer literals are treated as int values. To explicitly indicate that an integer literal represents a value of type uint, use the suffix U/u, for type long use the suffix L/l, and for type ulong use the suffix UL/ul:

Uint a = 10U; long b = 20L; ulong c=30UL;

Using System Types

Above, when listing all the basic data types, the system type was mentioned for each. Because the name of a built-in type is essentially a shorthand for a system type. For example, the following variables will be equivalent in type:

Int a = 4; System.Int32 b = 4;

Implicit typing

Previously, we explicitly specified the type of variables, for example, int x; . And the compiler, when launched, already knew that x stores an integer value.

However, we can also use the implicit typing model:

Var hello = "Hell to World"; var c = 20; Console.WriteLine(c.GetType().ToString()); Console.WriteLine(hello.GetType().ToString());

For implicit typing, instead of the data type name, use keyword var. Then, during compilation, the compiler itself infers the data type based on the assigned value. The example above used the expression Console.WriteLine(c.GetType().ToString()); , which allows us to find out the inferred type of variable c. Since by default all integer values ​​are treated as int values, the variable c will end up being of type int or System.Int32

These variables are similar to regular ones, but they have some limitations.

First, we can't first declare an implicitly typed variable and then initialize it:

// this code works int a; a = 20; // this code doesn't work var c; c= 20;

Second, we cannot specify null as the value of an implicitly typed variable:

// this code doesn't work var c=null;

Since the value is null, the compiler will not be able to infer the data type.

double or decimal

From the above list of data types, it is obvious that if we want to use numbers up to 256 in a program, then we can use byte type variables to store them. When using large values, we can take the type short, int, long. The same thing for fractional numbers - for ordinary fractional numbers you can take the float type, for very large fractional numbers - the double type. The decimal type stands out here in the sense that despite its larger bit capacity compared to the double type, the double type can store a larger value. However, a decimal value can contain up to 28 decimal places, whereas a double value can contain 15-16 decimal places.

Decimal is more often used in financial calculations, while double is more often used in mathematical operations. The general differences between these two types can be summarized by the following table.

Last update: 09/17/2017

Each variable has a specific type. And this type determines what values ​​a variable can have, what operations can be performed on it, and how many bytes in memory it will occupy. The following basic data types are defined in the C++ language:

    bool : boolean type. Can take one of two values: true and false. The memory footprint for this type is not precisely defined.

    char : Represents a single ASCII character. Occupies 1 byte (8 bits) in memory. Can store any value from -128 to 127, or from 0 to 255

    signed char : Represents a single character. Occupies 1 byte (8 bits) in memory. Can store any value from -128 to 127

    unsigned char : Represents a single character. Occupies 1 byte (8 bits) in memory. Can store any value from 0 to 255

    wchar_t : Represents a wide character. On Windows it takes up 2 bytes (16 bits) of memory, on Linux it takes 4 bytes (32 bits). Can store any value from the range from 0 to 65,535 (for 2 bytes), or from 0 to 4,294,967,295 (for 4 bytes)

    char16_t : Represents a single Unicode character. Occupies 2 bytes (16 bits) in memory. Can store any value from 0 to 65,535

    char32_t : Represents a single Unicode character. Occupies 4 bytes (32 bits) in memory. Can store any value from 0 to 4,294,967,295

    short : Represents an integer in the range –32768 to 32767. Occupies 2 bytes (16 bits) of memory.

    This type also has synonyms short int, signed short int, signed short.

    unsigned short: Represents an integer in the range 0 to 65535. Occupies 2 bytes (16 bits) of memory.

    This type also has a synonym unsigned short int .

    int: represents an integer. Depending on the processor architecture, it can occupy 2 bytes (16 bits) or 4 bytes (32 bits). The range of limit values ​​accordingly can also vary from –32768 to 32767 (with 2 bytes) or from −2,147,483,648 to 2,147,483,647 (with 4 bytes). But in any case, the size must be greater than or equal to the size of the short type and less than or equal to the size of the long type

    This type has synonyms signed int and signed .

    unsigned int : Represents a positive integer. Depending on the processor architecture, it may occupy 2 bytes (16 bits) or 4 bytes (32 bits), and because of this, the range of limit values ​​may vary: from 0 to 65535 (for 2 bytes), or from 0 to 4,294,967,295 (for 4 bytes).

    unsigned can be used as a synonym for this type

    long : Represents an integer in the range −2,147,483,648 to 2,147,483,647. Occupies 4 bytes (32 bits) of memory.

    This type also has synonyms long int , signed long int and signed long

    unsigned long: Represents an integer in the range 0 to 4,294,967,295. Occupies 4 bytes (32 bits) of memory.

    Has the synonym unsigned long int .

    long long : Represents an integer in the range −9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. Occupies typically 8 bytes (64 bits) of memory.

    Has synonyms long long int , signed long long int and signed long long .

    unsigned long long : Represents an integer in the range 0 to 18,446,744,073,709,551,615. Typically 8 bytes (64 bits) in memory.

    Has the synonym unsigned long long int .

    float : Represents a single-precision floating-point real number in the range +/- 3.4E-38 to 3.4E+38. Occupies 4 bytes (32 bits) in memory

    double : Represents a double precision floating point real number in the range +/- 1.7E-308 to 1.7E+308. Occupies 8 bytes (64 bits) in memory

    long double : Represents a double-precision floating-point real number of at least 8 bytes (64 bits). Depending on the size of the occupied memory, the range of valid values ​​may vary.

    void : type without value

Thus, all data types except void can be divided into three groups: character (char, wchar_t, char16_t, char32_t), integer (short, int, long, long long) and floating point number types (float, double, long double).

Character types

The types used to represent characters in the application are char, wchar_t, char16_t, and char32_t.

Let's define several variables:

Char c="d"; wchar_t d="c";

A char variable takes as its value one character in single quotes: char c ="d" . You can also assign a number from the range specified above in the list: char c = 120 . In this case, the value of variable c will be the character that has code 120 in the ASCII character table.

It is worth considering that to output wchar_t characters to the console, you should use not std::cout, but the std::wcout stream:

#include int main() ( char a = "H"; wchar_t b = "e"; std::wcout<< a << b << "\n"; return 0; }

In this case, the std::wcout stream can work with both char and wchar_t. And the std::cout stream for the wchar_t variable will output its numeric code instead of a character.

The C++11 standard added the char16_t and char32_t types, which are oriented towards using Unicode. However, threads for working with these types have not yet been implemented at the OS level. Therefore, if you need to display the values ​​of variables of these types to the console, you need to convert the variables to types char or wchar_t:

#include int main() ( char a = "H"; wchar_t b = "e"; char16_t c = "l"; char32_t d = "o"; std::cout<< a << (char)b << (char)c << (char)d << "\n"; return 0; }

In this case, when outputting, the variables are preceded by a cast operation to the char type - (char) , due to which the values ​​of the variables b, c and d are converted to the char type and can be output to the console using the std::cout stream.

Integer types

Integer types are represented by the following types: short, unsigned short, int, unsigned int, long, unsigned long, long long and unsigned long long:

Short a = -10; unsigned short b= 10; int c = -30; unsigned int d = 60; long e = -170; unsigned long f = 45; long long g = 89;

Types of floating point numbers

Floating-point and fractional number types are represented by float, double and long double:

Float a = -10.45; double b = 0.00105; long double c = 30.890045;

Data type sizes

The list above shows for each type the size it occupies in memory. However, it is worth noting that compiler developers can choose the size limits for types independently, based on the hardware capabilities of the computer. The standard sets only the minimum values ​​that should be. For example, for the int and short types the minimum value is 16 bits, for the long type - 32 bits, for the long double type. In this case, the size of the long type must be no less than the size of the int type, and the size of the int type must not be less than the size of the short type, and the size of the long double type must be greater than double. For example, the g++ compiler for Windows uses 12 bytes for long doubles, and the compiler built into Visual Studio and also runs under Windows uses 8 bytes for long doubles. That is, even within the same platform, different compilers may have different approaches to the sizes of certain data types. But in general, the sizes that are indicated above when describing data types are used.

However, there are situations when it is necessary to know exactly the size of a certain type. And for this, C++ has the sizeof() operator, which returns the size of the memory in bytes that the variable occupies:

#include int main() ( long double number = 2; std::cout<< "sizeof(number) =" << sizeof(number); return 0; }

Console output when compiling in g++:

sizeof(number) = 12

At the same time, when defining variables, it is important to understand that the value of a variable should not go beyond the limits outlined for its type. For example:

Unsigned short number = -65535;

The G++ compiler, when compiling a program with this line, will generate an error stating that the value -65535 is not within the range of valid values ​​for the unsigned short type and will be truncated.

In Visual Studio, compilation can proceed without errors, but the number variable will receive the value 2 - the result of converting the number -65535 to an unsigned short type. That is, again, the result will not be exactly what is expected. The value of a variable is just a collection of bits in memory that are interpreted according to a specific type. And for different types, the same set of bits can be interpreted differently. Therefore, it is important to consider the value ranges for a given type when assigning a value to a variable.

auto specifier

Sometimes it can be difficult to determine the type of expression. And according to the latest standards, you can let the compiler infer the type of the object itself. And the auto specifier is used for this. Moreover, if we define a variable with the auto specifier, this variable must be initialized with some value:

Auto number = 5;

Based on the assigned value, the compiler will infer the type of the variable. Uninitialized variables with the auto specifier are not allowed.

Data types

Data types are especially important in C# because it is a strongly typed language. This means that all operations are subject to strict type checking by the compiler, and illegal operations are not compiled. Consequently, strict type checking eliminates errors and increases the reliability of programs. To enforce type checking, all variables, expressions, and values ​​must be of a specific type. There is no such thing as a “typeless” variable in this programming language at all. Moreover, the type of a value determines the operations that can be performed on it. An operation that is legal for one data type may not be valid for another.

There are two general categories of built-in data types in C#: value types And reference types. They differ in the contents of the variable. Conceptually, the difference between the two is that a value type stores data directly, while a reference type stores a reference to a value.

These types are stored in different locations in memory: value types are stored in an area known as the stack, and reference types are stored in an area known as the managed heap.

Let's take a look value types.

Integer types

C# defines nine integer types: char, byte, sbyte, short, ushort, int, uint, long and ulong. But the char type is used primarily to represent characters and is therefore treated separately. The remaining eight integer types are for numeric calculations. Below are their range of numbers and bit depth:

C# Integer Types
Type Type CTS Bit size Range
byte System.Byte 8 0:255
sbyte System.SByte 8 -128:127
short System.Int16 16 -32768: 32767
ushort System.UInt16 16 0: 65535
int System.Int32 32 -2147483648: 2147483647
uint System.UInt32 32 0: 4294967295
long System.Int64 64 -9223372036854775808: 9223372036854775807
ulong System.UInt64 64 0: 18446744073709551615

As the table above shows, C# defines both signed and unsigned variants of the various integer types. Signed integer types differ from their unsigned counterparts in the way they interpret the most significant bit of the integer. Thus, if a program specifies a signed integer value, the C# compiler will generate code that uses the most significant bit of the integer as the sign flag. A number is considered positive if the sign flag is 0, and negative if it is 1.

Negative numbers are almost always represented by the two's complement method, whereby all the binary digits of the negative number are first inverted and then 1 is added to that number.

Probably the most common integer type in programming is int type. Variables of type int are often used for loop control, array indexing, and general purpose mathematical calculations. When you need an integer value with a larger range of representations than the int type, there are a number of other integer types available for this purpose.

So, if the value needs to be stored without a sign, then you can select uint type, for large signed values ​​- long type, and for large unsigned values ​​- type ulong. As an example, below is a program that calculates the distance from the Earth to the Sun in centimeters. To store such a large value, it uses a long variable:

Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 ( class Program ( static void Main(string args) ( long result; const long km = 149800000; // distance in km. result = km * 1000 * 100; Console.WriteLine(result); Console.ReadLine(); ) ) )

All integer variables can be assigned values ​​in decimal or hexadecimal notations. In the latter case, a 0x prefix is ​​required:

Long x = 0x12ab;

If there is any uncertainty as to whether an integer value is of type int, uint, long, or ulong, then default int is accepted. To explicitly specify what other integer type a value should have, the following characters can be appended to a number:

Uint ui = 1234U; long l = 1234L; ulong ul = 1234UL;

U and L can also be written in lowercase, although a lowercase L can easily be visually confused with the number 1 (one).

Floating Point Types

Floating-point types allow you to represent numbers with a fractional part. There are two types of floating point data types in C#: float And double. They represent numeric values ​​in single and double precision, respectively. Thus, the width of the float type is 32 bits, which approximately corresponds to the range of representation of numbers from 5E-45 to 3.4E+38. And the width of the double type is 64 bits, which approximately corresponds to the range of representation of numbers from 5E-324 to 1.7E+308.

The float data type is intended for smaller floating-point values ​​that require less precision. The double data type is larger than float and offers a higher degree of precision (15 bits).

If a non-integer value is hard-coded in the source code (for example, 12.3), then the compiler usually assumes that a double value is intended. If the value needs to be specified as a float, you will need to append the F (or f) character to it:

Float f = 12.3F;

Decimal data type

A decimal type is also provided to represent high-precision floating-point numbers. decimal, which is intended for use in financial calculations. This type has a width of 128 bits to represent numeric values ​​ranging from 1E-28 to 7.9E+28. You're probably aware that regular floating-point arithmetic is prone to decimal rounding errors. These errors are eliminated by using the decimal type, which allows numbers to be represented to 28 (and sometimes 29) decimal places. Because this data type can represent decimal values ​​without rounding errors, it is especially useful for financial-related calculations:

Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 ( class Program ( static void Main(string args) ( // *** Calculation of the cost of an investment with *** // *** fixed rate of return*** decimal money, percent; int i; const byte years = 15 ; money = 1000.0m; percent = 0.045m;

The result of this program will be:

Symbols

In C#, characters are represented not in 8-bit code, as in many other programming languages ​​such as C++, but in 16-bit code, called Unicode. Unicode's character set is so broad that it covers characters from almost every natural language in the world. While many natural languages, including English, French and German, have relatively small alphabets, some other languages, such as Chinese, use fairly large character sets that cannot be represented in 8-bit code. To overcome this limitation, C# defines type char, which represents unsigned 16-bit values ​​ranging from 0 to 65,535. However, the standard 8-bit ASCII character set is a subset of Unicode ranging from 0 to 127. Therefore, ASCII characters are still valid in C# .

Answer:
  1. Integer data types:

short int, unsigned short int, int, unsigned int, long, unsigned long.

  1. Floating point data types (corresponding to real types):

float, double, long double.

  1. Character data type:

char (signed char), unsigned char, wchar_t.

  1. Boolean data type:

bool.

  1. Enumerated data type (introduced in Visual C++):

enum.

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

In C++, the main integer data types are: short int, unsigned short int, int, unsigned int, long (long int), unsigned long (unsigned long int).

These data types represent values ​​from a set of integers. For example:

2 -100 398

Data types that begin with the unsigned prefix can only contain positive numbers.

Data of the short int, unsigned short int type takes up half as much memory space as data of the int, unsigned int type.

Data of type long, unsigned long takes up twice as much memory space as data of type int, unsigned int.

3. How to describe a variable named x of integer type in a program?

Answer:
int x; // signed integer

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

4. How to write the number 239 into a variable of integer type?

To do this, use the assignment operator, which is denoted by the symbol ‘=’.

Answer 1. Entering a number into a variable after its description.

int x; x = 239;

Answer 2. Entering a number into a variable during its description (initial initialization).

int x = 239;

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

Floating point data types allow you to represent values ​​from a set of real numbers. For example:

8.35 -990.399 239.0.

C++ has the following basic floating point data types: float, double, long double.

A variable of type double takes up 2 times more space in computer memory than a variable of type float.

Also, a long double type variable takes up 2 times more space in the computer memory than a double type variable.

6. How to describe a variable that takes a floating point value?

An example of describing variables of the float, double, long double types:

float f; double d; long double ld;

7. How to write numeric values ​​to a floating point variable?

An example of entering numeric data into floating point variables:

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

8. How to convert a float variable to an int type?

To do this, the type casting operation is used. In parentheses you need to indicate the name of the type to which the cast is being cast.

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

When using type casting operations, you need to take into account the restrictions that apply to types that take up less space in the computer's memory.

For example, a variable of type short int can represent a smaller range of numbers than variables of type float or double. The following listing overflows a value in a variable of type short int :

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

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

Example of casting from int to double:

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

10. What are the features of using char data (character data) in a program?

Data of type char represents the character value of the code entered from the keyboard. The symbol code is an integer.

For example, the code for the character 'f' is 102.

A code fragment that calculates the character code:

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

Data of type char are the same integers. Data of type char takes up 1 byte in computer memory.

The character-to-code relationship is located in the Windows symbol table. Characters with codes from 0 to 127 are reserved BIOS characters. They include the most commonly used symbols, numeric symbols, and Latin symbols. These characters cannot be changed.

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

11. What are the features of using data type bool (logical type)?

Variables of type bool can only take two values:

true - truth,

false - false.

These variables are used to test Boolean expressions. The numeric value of true is 1. The numeric value of false is 0 .

Code snippet that defines the numeric values ​​true and false :

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

Code snippet that converts int and float types to bool:

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

12. How to determine the size of memory occupied by a variable of a given type?

The sizeof() operation is used for this.

A code snippet that defines 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 are variables of different types initialized?

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

14. How to determine the maximum permissible (minimum permissible) value of a variable of a certain type?

To determine the maximum or minimum value for a variable of a certain type, the .NET Framework library uses the MaxValue and MinValue properties.

Examples of determining limit values ​​of variables of different types.

For variables of type int :

// type int 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 float variables:

// float type 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 using the enum type?

The enum type is an enumeration data type. It specifies mnemonic values ​​for sets of integer values. Each mnemonic value has a specific content and is represented by an integer.

An example of using the enum type to represent 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

The example below describes a variable named mn of type enum months. Mnemonic values ​​for months (January, February, …) start from 0 (0, 1, 2, …). The January mnemonic is the integer value 0, the February mnemonic is the integer value 1, and so on.

So, using the enum type, mnemonics can be used in the program text for better clarity of the source code.

You can also write it like this:

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

16. What are the features of the typevoid in programs onC++ ?

The void data type is used in the following cases:

  • if you need to describe a function that does not return any value (see example);
  • if you need to describe a function that does not receive parameters (see example).

Example. MyFun() is a parameterless function that returns no value (returns type void ) and takes no parameters.

public : void MyFun(void ) { // function body // ... return; // return from a function that does not return a value } // call a function from the program ... MyFun(); ...

17. Is it possible to declare a variable of typevoid in a programme?

This is not possible because the void type is not associated with a value.

Declaring a variable of type void results in a compilation error with the following message:

"Illegal use of type void"

18. What are the features of the typewchar_ t VVisual C++ ?

Variables of type char (see previous paragraphs) are used to store 8-bit ASCII characters.

The wchar_t type is used to store characters that are part of large character sets. For example, the Chinese alphabet has a huge number of characters. 8 digits are not enough to represent the entire set of Chinese characters. Therefore, if you need to use the program on the international market, it is advisable to replace the char type with wchar_t.

Example using the wchar_t type.

... wchar_t t; // 2 bytes of memory are allocated for the variable t t = "s"; ...

TO data refers to any information presented in a form that allows you to automate its collection, storage and processing in a computer (numbers, symbols, bits, etc.).
The data in the program can be original(set at the program input) or processing results(intermediate or weekend).
All data - variables and constants - belong to a specific type.
Each data type has its own associated range of values(number of bytes for one value) and valid operations.

Data types in C/C++ are divided into basic And derivatives.
The main types include:

  1. void(empty type),
  2. int(integer type),
  3. float(real floating point numbers),
  4. double(double precision floating point real numbers),
  5. char(character type),
  6. bool- logical.

Composite types

To generate other types of data use basic types + so-called specifiers. Data types created from standard types using specifiers are called composite data types. C++ defines four data type specifiers:
  1. short - short;
  2. long - long;
  3. signed-signed;
  4. unsigned
Derived types are:
  • arrays,
  • functions,
  • classes,
  • signs,
  • links,
  • structures,
  • associations.

Character type

Data type char always occupy 1 byte in computer memory. This is due to the fact that usually the amount of memory allocated for a character type value is as much as is necessary to store any of the 256 keyboard characters.
The character type can be with a sign or unsigned.
In signed quantities signed char you can store values ​​in the range from -128 to 127. Accordingly, the values ​​of variables like unsigned char can range from 0 to 255.
Data type Range of values Size
char -128...+127 1 byte
unsigned char 0...255 1 byte
signed char -128...127 1 byte

When working with character data, you need to remember that if an expression contains single character, it must be enclosed in single quotes. A sequence of characters, that is, a string, when used in expressions is in double quotes. For example: ‘F’, ‘3’, “Ivan”, “235”

Integer type

Data type int in computer memory can occupy either 2, 4 or 8 bytes. This depends on the processor bit size.
By default, all integer types are considered signed, that is, the specifier signed(sign) may not be specified.
Specifier unsigned(unsigned) allows only positive numbers to be represented.

Integer value ranges

Data type Range of values Size(bytes)
int
signed int
signed long int
-2147483648 ... 2147483647 4
unsigned int
unsigned long int
0 ... 4294967295 4
short int
signed short int
-32768 ... 32767 2
unsigned short int 0... 65535 2
long long int \(-(2^{63}-1)...(2^{63}-1)\) 8
unsigned long
long int
\(0...(2^{64}-1)\) 8

Real type

The internal representation of a real number in computer memory is different from the representation of an integer. A floating point number is represented by in exponential form. $$\pm mE\pm p$$ where m- mantissa (integer or fractional number with a decimal point), R- order (integer). In order to convert a number in exponential form to the usual fixed-point notation, you need to multiply the mantissa by ten to the power of order For example: \(-6.42E+2=-6.42*10^(2)\)

Value ranges of real types

Data type Range of values Size (bytes)
float 3.4E-38 ... 3.4E+38 4
double 1.7E-308... 1.7E+308 8
long double 3.4E-4932 ... 3.4E+4932 10

The length of the mantissa determines the precision of the number, and the length of the order of its range.
Float data takes up 4 bytes, of which 1 binary bit is allocated for the sign, 8 bits for the exponent and 23 for the mantissa. Since the most significant digit of the mantissa is always 1, it is not stored.
Data of the double type occupy 8 bytes, they have -11 bits for the order and -52 bits for the mantissa, respectively.
The long type specifier before the double type name indicates that 10 bytes are allocated for the value.

Boolean type

Type variable bool can only take two values true(true) or false(lie). Any non-zero value is interpreted as true, and when converted to an integer type it becomes 1. The value false is represented in memory as 0.

Type void

The value set of this type is empty.
The void type is used for:
  • definitions of functions that do not return a value;
  • to specify an empty list of function arguments;
  • as a base type for pointers;
  • in the type conversion operation.

Declaring Variables

Variable is a named area of ​​memory that stores a value of a specific type.
The variable has Name(identifier) ​​and meaning.
Name serves to access the memory area in which the value is stored.
Name(identifier) ​​is a collection of letters, numbers, and underscores that specifies the name of a variable, the name of a function, or a keyword in a program. The C/C++ language is case sensitive (i.e. Sum and sum will be treated as two different variables).
Where, type– a keyword that defines the amount of memory (number of bytes) allocated for storing the value of a variable (as a program object), (int – integer, float, double – real, char – symbolic, bool – logical);
Name– a unique variable identifier that specifies the symbolic address of the program object in computer memory;
initiator– the initial value of the variable, which may not be in the description.
For example: Variables of the same type can be grouped by separating them with “,”. Descriptions of variables of different types are separated by “ ; "
Depending on where they are declared, variables in the C/C++ language can be divided into three classes:
  1. local - declared inside a function and available only in it;
  2. global - described before all functions and accessible from anywhere in the program;
  3. formal parameters of functions are described in the list of function parameters.
Example: