C ++程序返回int类型,那么为什么返回-1返回255? [英] C++ Programs return int type, so why does return -1 return 255?

查看:649
本文介绍了C ++程序返回int类型,那么为什么返回-1返回255?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意,尽管我认为Windows(或其他)计算机上的结果没有不同,但我正在运行linux计算机.

Note that I am running a linux machine, although I don't think the result is different on a windows (or other) machine.

这是一个简单的问题-C ++程序通常返回32位int.如果我return -1,然后从终端打印结果,则结果为255.

This is a simple question - C++ programs usually return a 32 bit int. If I return -1, and then print the result from the terminal, the result is 255.

这是为什么?我觉得链接是我应该知道的,或者应该在很多年前就已经注意到了-我以前从未真正使用过返回码,也从未考虑过.

Why is this? I feel link this is something I should know, or should have noticed many years ago - I never really used return codes before now or thought about it before.

测试C ++程序:

int main()
{
    return -1;
}

编译:

g++ main.cpp -o a.out

运行:

./a.out

检查结果:

echo $?

结果:

255

我本来希望看到2 ^ 32-1.

I would have expected to see 2^32 - 1.

为什么结果255不是-1甚至是4294967295. (2^32 - 1)

Why is the result 255 not -1 or even 4294967295. (2^32 - 1)

推荐答案

因为(不是全部)操作系统使用了整个返回值.在这种情况下,它会被截断为低8位.操作系统(和其他相关组件,例如外壳)必须使用"和保留"该值,这就是所谓的实现细节",换句话说,C和C ++标准不区分什么返回值的有用性或含义是[除了以下段落之外]-只是从C的角度来看,它是int-C程序可能在忽略,截断,扩展或乘以该值的环境中启动到432为止,它仍然是有效的C或C ++环境.

Because (not all) OS's use the whole return value. In this case, it's truncated to the low 8 bits. It's up to the OS (and other related components, such as the shell) to "use" and "retain" this value, and it's what's called an "implementation detail", in other words, the C and C++ standards do not dicate what the usefulness or meaning of the return value is [aside from the paragraph below] - just that from the C perspective it is an int - the C program may be started in an environment where that value is ignored, truncated, extended or multiplied by 432 and it's still a valid C or C++ environment.

C标准规定,值0EXIT_SUCCESS(据我所知应为零)和值EXIT_FAILURE(某些非零值)应分别视为成功和失败.但是,所有其他值都是实现定义的",因此,在发生执行的OS/环境的规则下.

The C standard says that the value 0 or EXIT_SUCCESS (which should have the value zero as I understand it) and the value EXIT_FAILURE (some non-zero value) should be considered success and failure respectively. However, all other values are "implementation defined" and thus under the rules of the OS/environment that the execution happens in.

请注意,当外壳程序(或运行程序的任何程序)运行时,它不会直接跳转到main,而是首先执行一些其他功能来初始化main函数所需的内容.一旦main返回/退出,通常还有一些代码也会在程序执行后执行.确切的工作方式取决于几件事:

Note that when the shell (or whatever runs your program), it does not jump straight to your main, but some other functionality is performed first to initialize things that are needed by your main function. And once main has returned/exited, there is usually some code that executes AFTER your program as well. Exactly how this works is dependent on several things:

  • 谁编写了编译器和/或运行时库
  • 它设计用于什么操作系统
  • 这是什么处理器架构
  • 潜在的shell/运行时/操作系统功能启动了该过程

C和C ++标准未定义这些内容,因为这样做可能会影响可以/将运行C和/或C ++应用程序的实际平台,并且C和C ++的目标是具有包容性"-换句话说,请尝试不限制语言所支持的环境,处理器等.

The C and C++ standard doesn't define these things, as doing so would potentially affect the actual platforms that can/will run C and/or C++ applications, and the goal of C and C++ is to "be inclusive" - in other words, try to not limit the environment, processor, etc that the languages support.

这篇关于C ++程序返回int类型,那么为什么返回-1返回255?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆