問題描述
為什麼系統不返回主值? (Why system doesn't return main's value?)
[root@ test]$ cat return10.c
#include <stdio.h>
int main(int argc, char *argv[]){
return 10;
}
[root@ test]$ perl -e 'print system("/path_to_return10")'
2560
I was expecting 10 but got 2560,why?
參考解法
方法 1:
as specified in the documentation for the system
call in perl (http://perldoc.perl.org/functions/system.html):
The return value is the exit status of the program as returned by the wait call. To get the actual exit value, shift right by eight (see below).
indeed: 2560 >> 8 = 10
方法 2:
See $?
in perldoc perlvar.
You got 10 * 256 (return value = 10) + 0 * 128 (there was no core dump) + 0 (process wasn't killed by signal).
(by asker、Adrien Plisson、Dallaylaen)