0

I have this as my program and I want to call that program:

int main(int argc,char *argv[]) {
    int x = atoi(argv[1]);
    return x;
}

I call that program with another program and give it an int argument. It works good when x is low but lets say x = 1000 then it returns a low number

Daheh
  • 137
  • 1
  • 1
  • 11

3 Answers3

4

The main return value is passed to the exit function.

The exit function's argument is declared as an int, and you can legally pass any int value to it. On UNIX-like systems, all but the low-order 8 bits of the argument are quietly ignore. For example, exit(INT_MAX); is perfectly legal, but it's equivalent to exit(255); (or exit(-1); for that matter).

In Windows you have 32 bits, but one particular value (STILL_ACTIVE = 259 which means process still running) that you must avoid just above the byte value range.


The C++ standard defines just three possible values: 0 (which means success), EXIT_SUCCESS and EXIT_FAILURE, the latter two macro symbols from <stdlib.h> (where also the exit function is declared).

In Unix-land and Windows EXIT_SUCCESS is defined as 0, which is allowed by the standard.

Unfortunately, in Windows, where it's common to use the process exit code to communicate a low level error code, EXIT_FAILURE is typically/always defined as 1 instead of as E_FAIL. Which means that even for a program documented as returning an error code or HRESULT as exit code, you have to beware of the value 1. It's possibly just EXIT_FAILURE.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1
    Not *quite* accurate. The `exit()` function's argument is of type `int`, and; in Unix-land, all but the low-order 8 bits are ignored. It's perfectly legal to call `exit(INT_MAX)`, but it's equivalent to `exit(255)`. – Keith Thompson Jul 21 '14 at 18:34
  • @KeithThompson: thanks, I didn't think of that (that it mattered). Will rewrite. OK, done. – Cheers and hth. - Alf Jul 21 '14 at 18:35
  • Your second paragraph (now edited) still says that the argument is *limited* to an 8-bit byte value range, which implies that passing a value outside that range is somehow invalid. – Keith Thompson Jul 21 '14 at 18:36
  • @KeithThompson: well, can you fix it? i not master words this english luangage! thanks! :) – Cheers and hth. - Alf Jul 21 '14 at 18:38
2

The value returned by main is actually just a single unsigned byte. The high bits are used for other things (special status flags).

At least on UNIX and UNIX-like systems (like Mac OSX and Linux). See e.g. the official POSIX reference for the wait system call.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • **-1** "The value returned by main is actually just a single unsigned byte" is incorrect for Windows. I'm -1'ing in spite of the qualification in next paragraph. It's still very misleading. – Cheers and hth. - Alf Jul 21 '14 at 18:29
2

On Posix and Unix systems the return value of main matters mostly as 0 (i.e. EXIT_SUCCESS) vs non zero (e.g. EXIT_FAILURE i.e. 1) -and as others are explaining, only the lower byte matters. This is what every shell can handle very easily (e.g. with its if or its &&); to get the exit code in a shell you otherwise need $?

BSD systems have some dozen of (single byte) conventional exit codes in <sysexits.h> (but Linux usually don't follow these conventions).

On Unix systems the good way to "return" an integer i from a program is to output it (in textual form, i.e. with printf("%d\n", i); ...) to stdout. The the invoking program (a shell or something else) could read that. In a C program it is quite easy to use popen(3) (with pclose) or more generally learn about pipe(7)-s

My personal dream would be to have program returning some string as their exit value, but this won't happen soon and require changes in OS and compilers and languages....

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547