[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: Help please!!



Satheesh Kumar. N.K forced the electrons to say:
> If the function main() returns a value with return x; - who gets this
> return value? Is this return value stored somewhere?

The return value of main() is available to the parent of your program as
the exit status of the program. In most cases, the parent is the shell,
and thus the shell knows that your program returned a particular value
(the shell variable $? stores the return value of the last exited
foreground process).

A typical piece of code will be:

int a;
pid_t p;
p = fork();
switch (p) {
   case -1:
      perror ("fork");
      exit (1);
   case 0:
      /* Any setup before the new program runs */
      execl ("/home/binand/a.out", "/home/binand/a.out", NULL);
      perror ("execl");
      exit (1);
   default:
      waitpid (p, &a, 0);
      if (WIFEXITED (a)) {
	 printf ("Program a.out exited with status %d\n", WEXITSTATUS (a));
	 /* Whatever postprocessing */
      }
}

What WEXITSTATUS() returns is the exit status of your program - either as a
return value from main(), or as the argument to an exit() call. This is what
is available to the parent process.

For more details, read the man pages of fork, execl, wait etc.

This is almost the way a shell works.

Binand

-- 
The prompt for all occasions:
export PS1="F:\$(pwd | tr '/[a-z]' '\134\134[A-Z]')> "
--------------- Binand Raj S. (binand@xxxxxxxxxxxxxxxxxxxxx)