[완료] main()의 리턴값을 취득하려고 했는데, 의외의 결과가 나와서 글을 씁니다.

sia79의 이미지

소스입니다.

a.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main( void )
{
  printf("return 11111;\n");
  return 11111;
}
 
b.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int main( void )
{
  printf("a.out of b.out = %d\n", system("./a.out") );
  return 99999;
}

결과입니다.
[root@localhost ~]# ./a.out
return 11111;
[root@localhost ~]# echo $?
103
[root@localhost ~]# ./b.out
return 11111;
a.out of b.out = 26368
[root@localhost ~]# echo $?
159

예상했던 값은 11111, 99999 였습니다만... 왜 이런 결과가 나오게 된 건지 알수가 없습니다.

main함수의 리턴값을 제대로 받으려면 어떻게 해야하나요?

fedora5 gcc 환경입니다.

bushi의 이미지

EXIT_FAILURE, EXIT_SUCCESS 외엔 의미가 없습니다.
stdlib.h

OTL

thyoo의 이미지

http://www.opengroup.org/onlinepubs/000095399/functions/_exit.html

The value of status may be 0, EXIT_SUCCESS, EXIT_FAILURE, [CX] [Option Start] or any other value, though only the least significant 8 bits (that is, status & 0377) shall be available to a waiting parent process. [Option End]

LSB 8bit만 부모 프로세스에 전달됩니다.

11111d = 2B67h

return 0x2B67

-> 0x67 = 103d

___________________________________
Less is More (Robert Browning)

___________________________________
Less is More (Robert Browning)