• C

How to get the return code of program in C?

Is it possible to get the return code of program if I use "system" function to call the program?
tanAsked:
Who is Participating?
 
Andrei RodionovConnect With a Mentor Commented:
Hello tan!

In order to get exit code in parent program (A) from child program (B) you must use one of the spawn... functions. For example:

file a.c
#include <process.h>
#include <stdio.h>

int main()
{
  printf( "b return code is %d\n", spawnl( P_WAIT, "b", NULL ) );
  return 0;
}

file b.c
#include <stdio.h>

int main()
{
  printf( "I'm b program...\n" );
  return 1;
}

Just make them and check results.


Andrew Rodionov
0
 
arnondCommented:
If you're using, DOS, you cann't system() returns 0 if it's successfull and -1 on error and sets 'errno' to an error message relvant to the failure.
In UNIX, it should work. I'll try and get back to you.

Arnon David.
0
 
arnondCommented:
No. It doesn't work on UNIX too.

Sorry,
Arnon.
0
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
tanAuthor Commented:
I think I don't describe my question clearly. Assume that I have two programs, A and B. In program A, I use "system" function to call B. B will return a integer value by using "exit" or "return" statement in main function. Here is the question. Is it possible for A to get the return code from B?
0
 
ozoCommented:
yes
0
 
ZoppoCommented:
Hi tan,

For DOS and Windows it should be possible to read the ERRORLEVEL variable to get the exit-status (somehow like 'echo %ERRORLEVEL%' in a dos box). Not sure what's similar in UNIX.

ZOPPO
0
 
arnondCommented:
ozo, I tried runing these code segments and got some wrong outputs (on a UNIX machine)

a:
#include <stdio.h>
int main()
{
  int i;
  printf ("Enter number:\n");
  scanf ("%d",&i);
  return (i);  /*or also, exit (i), both didn't work.*/
}

b:
#include <stdio.h>
{
  int i=0;
  while (i!=99)
  {
     i=system("a");
     printf ("Got %d from a\n",i);
  }
  return (0);
}

and I get strange outputs from the printf() in b. what did I do wrong ?

Arnon.
0
 
ozoCommented:
b: is missing main, but with that fixed, I get the expected output:

Enter number:
0
Got 0 from a
Enter number:
1
Got 256 from a
Enter number:
2
Got 512 from a
Enter number:
3
Got 768 from a
Enter number:
4
Got 1024 from a
Enter number:

0
 
sreenathkCommented:
I also got the correct answers when I tried on DOS machine. But I did not tried on the UNIX machine. Just check the correct code in the UNIX machine.
Sreenath
0
 
arnondCommented:
ozo, of course.... I forgot the int main() part. I got the same exact output as you got. why ?

Andrew, which OS do you use ?

Arnon.
0
 
myokomizCommented:
In Unix system command can return :

-1 if it could not execute your program
or
an integer. This return can be divided in two. Most significant byte is
  the exit code of your program executed by a system call and the less
  significant means:
     0 if the program terminated normally
  or
     signal number if it was terminated by a signal

So if you want catch the exit code of your program you must do like this

  printf ("Got %d from a\n", i/256 );
0
 
sreenathkCommented:
I will give the code which I tried on the DOS machine. It is very simple. I did not use any spawnl().
File testa.c
#include <stdio.h>
int main()
{
      int i;
      scanf("%d",&i);
      return(i);
}

File testb.c
#include <stdio.h>
#include <stdlib.h>

int main()
{
      int i;
      i = system("testa");
      printf("Got value from testa as %d\n",i);
      return 1;
}

And gave the correct results also. I mean what ever number I entered I got it back.
Sreenath
0
 
ozoCommented:
arnond, were you expecting output like this?

#include <sys/wait.h>
printf ("Got %d from a\n",WEXITSTATUS(i));
0
 
Andrei RodionovCommented:
for arnold:

Well, my example works under DOS and I compiled it by Borland C++ 3.1. On UNIX platform you can't use spawn... functions.

Andrew Rodionov
0
 
arnondCommented:
ozo, I didn't but maybe myokomiz has a point, we got i*256 as the output.
Andrew and threenathk, that's strange, the help on system (borland c 3.1) says:
--------------------------------------------------------------------------------------------
Declaration:  int system(const char *command);

Remarks:
system invokes the DOS command interpreter file from inside an executing C program to execute a DOS command, batch file, or other program named by the "string "command.

To be located and executed, the program must be in the current directory or in one of the directories listed in the PATH string in the environment.

Because the COMSPEC environment variable is used to find the command interpreter file, the command interpreter file does not need to be in the current directory.

Return Value
On success, returns 0  
On error, returns -1 and sets errno to  
ENOENT, ENOMEM, E2BIG, or ENOEXEC, MEM, E2BIG, or ENOEXEC.
--------------------------------------------------------------------------------------------

so you can see why I said what I said.

Arnon. (not arnold....)
0
 
Andrei RodionovCommented:
for Arnon:

I'm sorry for my clumsiness: you are definitely Arnon not arnold.

And in DOS under Turbo C/Borland C++ spawn... functions work!

Andrew Rodionov
0
 
Andrei RodionovCommented:
So, myokomiz is right: for UNIX platform `system' return value contains the exit code in the high-order byte.

Andrew Rodionov
0
 
arnondCommented:
Andrew, I guess now that I'm familiar with spawnl, this is the way to do in DOS. We'll just have to keep looking for something similar in UNIX (unless tan wouldn't need it....)

Arnon.
0
 
arnondCommented:
too bad that we cann't get high exit values. system returns an int and if we want to return high values (like 1000 for example), we get an overflowed resault.

Arnon.
0
 
Andrei RodionovCommented:
Arnon, I think the return code mechanism was invented for small alternatives exactly. In order to exchange data between programs we must use IPC (interprocess communication).

Andrew Rodionov
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.