C++ C++;超过整数限制的安全措施

C++ C++;超过整数限制的安全措施,c++,integer,C++,Integer,我正在写一本书的章节回顾:在这一章的结尾,有一些问题/任务需要你完成 我决定以程序的格式而不是文本文件的格式执行这些操作: #include <iostream> int main(int argc, char* argv[]) { std::cout << "Chapter review\n" << "1. Why does C++ have more than one integer type?\n" << "\tTo

我正在写一本书的章节回顾:在这一章的结尾,有一些问题/任务需要你完成

我决定以程序的格式而不是文本文件的格式执行这些操作:

#include <iostream>

int main(int argc, char* argv[]) {
std::cout << "Chapter review\n"
    << "1. Why does C++ have more than one integer type?\n"
        << "\tTo be able to represent more accurate values & save memory by only allocating what is needed for the task at hand.\n"

    << "2. Declare variables matching the following descriptions:\n"
        << "a.\tA short integer with the value 80:\n";
            short myVal1 = 80;
std::cout << "\t\t\"short myVal1 = 80;\": " << myVal1 << std::endl

        << "b.\tAn unsigned int integer with the value 42,110:\n";
            unsigned int myVal2 = 42110;
std::cout << "\t\t\"unsigned int myVal2 = 42110;\": " << myVal2 << std::endl

        << "c.\tAn integer with the value 3,000,000,000:\n";
            float myVal3 = 3E+9;
std::cout << "\t\t\"float myVal3 = 3E+9;\": " << static_cast<unsigned int>(myVal3) << std::endl

    << "3. What safeguards does C++ provide to keep you from exceeding the limits of an integer type?\n"
        << "\tWhen it reaches maximum number it starts from the begging again (lowest point).\n"

    << "4. What is the distinction between 33L and 33?\n"
            << "\t33L is of type long, 33 is of type int.\n"

    << "5. Consider the two C++ statements that follow:\n\tchar grade = 65;\n\tchar grade = 'A';\nAre they equivalent?\n"
            << "\tYes, the ASCII decimal number for 'A' is '65'.\n"

    << "6. How could you use C++ to find out which character the code 88 represents?\nCome up with at least two ways.\n"
            << "\t1: \"static_cast<char>(88);\": " << static_cast<char>(88) << std::endl; // 1.
            char myChar = 88;
std::cout   << "\t2: \"char myChar = 88;\": " << myChar << std::endl // 2.
            << "\t3: \"std::cout << (char) 88;\"  " << (char) 88 << std::endl // 3.
            << "\t4: \"std::cout << char (88);\": " << char (88) << std::endl // 4.

    << "7. Assigning a long value to a float can result in a rounding error. What about assigning long to double? long long to double?\n"
            << "\tlong -> double: Rounding error.\n\tlong long -> double: Significantly incorrect number and/or rounding error.\n"

    << "8. Evaluate the following expressions as C++ would:\n"
        << "a.\t8 * 9 + 2\n"
            << "\t\tMultiplication (8 * 9 = 72) -> addition (72 + 2 = 74).\n"
        << "b.\t6 * 3 / 4\n"
            << "\t\tMultiplication (6 * 3 = 18 -> division (18 / 4 = 4).\n"
        << "c.\t3 / 4 * 6\n"
            << "\t\tDivision (3 / 4 = 0) -> multiplication (0 * 6 = 0).\n"
        << "d.\t6.0 * 3 / 4\n"
            << "\t\tMultiplication (6.0 * 3 -> 18.0) -> division (18.0 / 4 = 4.5).\n"
        << "e.\t 15 % 4\n"
            << "\t\tDivision (15 / 4 = 3.75) Then returns the reminder, basically how many times can 4 go into 15 in this case that is 3 (3*4 = 12).\n"

    << "9. Suppose x1 and x2 are two type of double variables that you want to add as integers and assign to an integer variable. Construct a C++ statement for doing so. What if you wanted to add them as type double and then convert to int?\n"
        << "\t1: \"int myInt = static_cast<double>(doubleVar);\"\n\t2: \"int myInt = int (doubleVar);\".\n"

    << "10. What is the variable type for each of the following declarations?\n"
        << "a.\t\"auto cars = 15;\"\n\t\tint\n"
        << "b.\t\"auto iou = 150.37f;\"\n\t\tfloat\n"
        << "c.\t\"auto level = 'B';\"\n\t\tchar\n"
        << "d.\t\"auto crat = U'/U00002155';\"\n\t\twchar_t ?\n"
        << "e.\t\"auto fract = 8.25f/.25;\"\n\t\tfloat" << std::endl;

        return 0;
}
#包括
int main(int argc,char*argv[]){

std::cout基本上我不能接受一条评论作为答案,所以总结一下:

没有保障措施,我误解了@n.m.为我澄清的问题

10.e是错误的,正如@Jarod42所指出的,这是正确的

谢谢!

对于我来说,“声明值为3000000000的整数变量”是:


< C++ >第十一C++提供15个整数类型,未签名int是最小的,可以存储一个大整数,如3×000 000 000。< /P> < P> C++将整数溢出归类为“未定义行为”-任何可能发生的结果。 <> GCC有“<代码> -fTrpv编译开关,当整数溢出发生时,程序会崩溃。这允许您调试溢出。该特性是可能的,因为C++使其合法(本质上是未定义行为)。为了使你的程序在这些环境下崩溃。我认为C++委员会在制定C++标准的那一部分时会考虑到这个精确的场景。


这与Java不同,Java中的整数溢出会导致概括,并且可能更难调试。

很高兴看到您在自学……通过实际操作……这不是一种保护措施。默默地忽略错误并做一些愚蠢的事情,比如翻滚,而不是引发正确的错误,这是不安全的,并且会让您免受任何伤害.这个问题的正确答案是没有。(续)问:你有什么样的安全措施来防止游客在屋顶的边缘行走?答:重力。任何试图在边缘行走的人都不会成功,他们会因为重力而坠落。任务完成了:没有人走到屋顶的边缘!你同意吗?10。:
8.25f/.25
,类型是
double
(as
.25
)而不是
float
    unsigned anInteger = 3000000000;