Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to get the return code of program in C?

Posted on 2000-02-22
20
Medium Priority
?
446 Views
Last Modified: 2010-04-15
Is it possible to get the return code of program if I use "system" function to call the program?
0
Comment
Question by:tan
  • 7
  • 5
  • 3
  • +4
20 Comments
 
LVL 3

Expert Comment

by:arnond
ID: 2545309
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
 
LVL 3

Expert Comment

by:arnond
ID: 2545336
No. It doesn't work on UNIX too.

Sorry,
Arnon.
0
 

Author Comment

by:tan
ID: 2545402
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

Expert Comment

by:ozo
ID: 2545411
yes
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2545422
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
 
LVL 3

Expert Comment

by:arnond
ID: 2545479
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
 
LVL 85

Expert Comment

by:ozo
ID: 2545504
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
 
LVL 2

Accepted Solution

by:
AndrewRodionov earned 300 total points
ID: 2545542
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
 
LVL 3

Expert Comment

by:sreenathk
ID: 2545567
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
 
LVL 3

Expert Comment

by:arnond
ID: 2545569
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
 

Expert Comment

by:myokomiz
ID: 2545572
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
 
LVL 3

Expert Comment

by:sreenathk
ID: 2545581
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
 
LVL 85

Expert Comment

by:ozo
ID: 2545593
arnond, were you expecting output like this?

#include <sys/wait.h>
printf ("Got %d from a\n",WEXITSTATUS(i));
0
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2545625
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
 
LVL 3

Expert Comment

by:arnond
ID: 2545646
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
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2545670
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
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2545705
So, myokomiz is right: for UNIX platform `system' return value contains the exit code in the high-order byte.

Andrew Rodionov
0
 
LVL 3

Expert Comment

by:arnond
ID: 2545717
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
 
LVL 3

Expert Comment

by:arnond
ID: 2545730
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
 
LVL 2

Expert Comment

by:AndrewRodionov
ID: 2545753
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question