C++: How can I return a negative value in main.cpp

前端 未结 3 1741
鱼传尺愫
鱼传尺愫 2020-12-30 00:29

As an assignment in school, we have to write a C++ program and returns different error codes in the main.

The problem is that we have to return -2

3条回答
  •  一生所求
    2020-12-30 01:20

    The problem is that what is returned to the OS is then interpreted by the OS shell as IT likes it, not as your program likes.

    the main function returns an int, and return -2 is just what your program has to do.

    253 is -2 in 2s complement onto 8 bits.

    The problem -here- is a mismatch between the C++ specs (int main()) and the way the shell use it. But it does not depend on the program.

    The assignment itself is a trap.

提交回复
热议问题