Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

3.4.4Short Variables

If you need to represent a number that is a little higher than the Byte can hold, you can declare it using the short keyword. Here is an example:

using namespace System;

Int main()

{

short Pages = 424;

Console::WriteLine(Pages);

return 0;

}

A short variable can hold a natural number whose value ranges from -32768 to 32767. Based on this, it is important to note that a Byte value can fit in a short. Therefore, it is normal to declare as short a variable that would hold even small numbers such as people's ages.

When assigning a value to the variable, you can use a number in that range. If you assign a value out of that range, you would receive a warning and the number would be "truncated". That is, the number would be converted to the value of the other extreme. Consider the following program:

using namespace System;

Int main()

{

short Number = 32769;

Console::WriteLine(Number );

Console::WriteLine();

return 0;

}

This would produce the following warning:

warning C4309: 'initializing' : truncation of constant value

The program would produce:

-32767

Press any key to continue

If you want to enforce the idea that the variable is signed, you can type the signed keyword before short. Here is an example:

using namespace System;

Int main()

{

signed short Number = -326;

Console::WriteLine(Number );

Console::WriteLine();

return 0;

}

If you want the variable to hold only positive numbers, you can type the unsigned keyword to the left of short and make sure you omit the signed keyword. When using the unsigned keyword, you can store numbers that range from 0 to 65535. If you assign either a negative number or a number higher than 65535, the number would be truncated. This time, the truncation is done differently but the result is not as intended. Here is an example:

using namespace System;

Int main()

{

unsigned short Number = -326;

Console::WriteLine(Number );

return 0;

}

After a warning, this would produce:

65210

Press any key to continue

Besides the short keyword, to declare a variable for a relatively small number, you can use the Int16 data type. The Int16 data type is defined in the System namespace. Its variable can hold values in the same range as the short. Here is an example:

using namespace System;

Int main()

{

System::Int16 Number = -326;

Console::WriteLine(Number );

Console::WriteLine();

return 0;

}

Because an Int16 variable can hold negative values, if you want to use only positive values, you can declare the variable using the UInt16 data type (The U stands for "unsigned"). The UInt16 data type is defined in the System namespace. Here is an example:

using namespace System;

Int main()

{

System::UInt16 Number = 60326;

Console::WriteLine(Number );

Console::WriteLine();

return 0;

}

This would produce:

60326

Press any key to continue

3.4.5Integral Variables

In some cases you may need a variable that hold a number larger than the short or the Int16 can carry. To declare such a variable, you can use the int keyword. The int data type is used for a variable whose value can range from –2,147,483,648 to 2,147,484,647. Here is an example:

using namespace System;