473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Return value of system

Hi all,
To test the return value of system I wrote 2 programs which are shown
below.

Program 1:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int ret;

ret = system("./TEST");
printf("Return value = %d\n",ret);

return ret;
}

Program 2:

int main()
{
return 50;
}

The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?

Thanks

Jan 18 '06 #1
13 7534
On 17 Jan 2006 21:03:59 -0800, va******@rediffmail.com wrote in
comp.lang.c:
Hi all,
To test the return value of system I wrote 2 programs which are shown
below.

Program 1:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int ret;

ret = system("./TEST");
printf("Return value = %d\n",ret);

return ret;
}

Program 2:

int main()
{
return 50;
}

The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?

Thanks


Doesn't your compiler provide documentation, online help, or man
pages?

Did you try typing

"system()" function

into Google?

The system() function is almost completely implementation-defined. The
strings they pass to it, what they mean, how they are interpreted, and
what happens as a result, are all implementation-defined.

With one exception, that is. If you call system() with a null pointer
as an argument, the return value tells you whether or not there is a
command shell available. If system(NULL) returns 0, there is no
command shell that you can use. If system(NULL) returns a non-zero
value, there is a command shell.

Beyond that, since it is completely implementation-defined, you need
to consult the documentation for your implementation (compiler), which
is required to document what it will return when you call it.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jan 18 '06 #2
Is there any way to get the value returned from Program2 and print it
in program1?

Jan 18 '06 #3

change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);

Jan 18 '06 #4
va******@rediffmail.com wrote:
Is there any way to get the value returned from Program2 and print it
in program1?


Can you answer a question for me first? Do you know if my car is
front-wheel drive or rear-wheel drive? I would really like to know,
thanks.

- Logan
Jan 18 '06 #5
> Can you answer a question for me first? Do you know if my car is
front-wheel drive or rear-wheel drive? I would really like to know,

Yes, I can ask get that information from you.

Jan 18 '06 #6
> Can you answer a question for me first? Do you know if my car is
front-wheel drive or rear-wheel drive? I would really like to know,

Yes, I can get that information from you.

Jan 18 '06 #7
an********@gmail.com writes:
change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);


Why would you want to do that?

Since I just read the article to which you're replying, I happen to
know that ret is the value returned by a call to system(). Please
read <http://cfaj.freeshell.org/google/> to learn how to make sure
everyone knows what you're talking about.

The value returned by system(), with one exception, is entirely
implementation-defined. There is no reason to think that ret%255 is
going to be meaningful; on any implementation I know about, it isn't.
(I think I know what you're referring to, but you've gotten it wrong.)

For information on what system() returns on a given implementation,
consult the documentation for that implementation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 18 '06 #8
an********@gmail.com wrote:
change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);


First, please go to <http://cfaj.freeshell.org/google/> and follow the
instructions there the next time you post a followup via Google Groups.

Second, the return value of system() is implementation defined. I
don't know of any system where your suggestion would be useful and if
there was one it wouldn't be on-topic here.
<OT> There are some systems where the return value of the command is an
8-bit value stored in the high byte of a 16-bit integer where dividing
by 256 will yield the original return value, maybe this is what you
were thinking of.</OT>

Robert Gamble

Jan 18 '06 #9

<va******@rediffmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi all,
To test the return value of system I wrote 2 programs which are shown
below.

Program 1:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int ret;

ret = system("./TEST");
printf("Return value = %d\n",ret);

return ret;
}

Program 2:

int main()
{
return 50;
}

The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?

Thanks


Program 1: (fscanf might not be the best thing to use to read the pipe, but
it demonstrates the principal.
Also needs more error checks:

#include <stdio.h>
int main(void) {
int n;
FILE *fp = popen( "./TEST;echo $?", "r" );
if ( fp ) {
fscanf( fp, "%d", &n );
printf( "%d\n", n );
pclose(fp);
}
}

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jan 18 '06 #10
"Fred Kleinschmidt" <fr******************@boeing.com> wrote in message
news:It********@news.boeing.com...
<va******@rediffmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
Hi all,
To test the return value of system I wrote 2 programs which are shown
below.
Program 1:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int ret;
ret = system("./TEST");
printf("Return value = %d\n",ret);
return ret;
}
Program 2:
int main()
{
return 50;
}
The executable name of program2 is TEST. I thought system in Program1
will return whatever program2 will return, but It didn't. What
exactly system will return?What should I do if I want to get the exact
return value of program2?
Thanks

Program 1: (fscanf might not be the best thing to use to read the pipe,
but it demonstrates the principal.
Also needs more error checks:

#include <stdio.h>
int main(void) {
int n;
FILE *fp = popen( "./TEST;echo $?", "r" );
if ( fp ) {
fscanf( fp, "%d", &n );
printf( "%d\n", n );
pclose(fp);
}
}


Wow... that's the dumbest thing I've ever seen!
Please tell me you're kidding.

Surely a system which supports popen() also
provides the WIFEXITED() and WEXITSTATUS()
macros. Wouldn't it be easier to use those on
the value returned by system()?
Unfortunately, clc is not the appropriate forum
to discuss these POSIX features...
perhaps comp.unix.programmer would be a
more appropriate forum.

Mark
Jan 18 '06 #11
On 17 Jan 2006 21:35:48 -0800, in comp.lang.c ,
va******@rediffmail.com wrote:
Is there any way to get the value returned from Program2 and print it
in program1?


Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jan 19 '06 #12
Robert Gamble wrote:
an********@gmail.com wrote:
change the line:
printf("Return value = %d\n",ret); => printf("Return value =
%d\n",ret%255);
First, please go to <http://cfaj.freeshell.org/google/> and follow the
instructions there the next time you post a followup via Google Groups.

Thanks for the pointer Second, the return value of system() is implementation defined.
agreed
II don't know of any system where your suggestion would be useful and if
there was one it wouldn't be on-topic here.
linux and solaris. As i don't have access to others
<OT> There are some systems where the return value of the command is an
8-bit value stored in the high byte of a 16-bit integer where dividing
by 256 will yield the original return value, maybe this is what you
were thinking of.</OT>


yup
anki

Jan 19 '06 #13
an********@gmail.com writes:
Robert Gamble wrote:
an********@gmail.com wrote:
> change the line:
> printf("Return value = %d\n",ret); => printf("Return value =
> %d\n",ret%255);


First, please go to <http://cfaj.freeshell.org/google/> and follow the
instructions there the next time you post a followup via Google Groups.

Thanks for the pointer
Second, the return value of system() is implementation defined.


agreed
I
I don't know of any system where your suggestion would be useful and if
there was one it wouldn't be on-topic here.


linux and solaris. As i don't have access to others


<OT>
ret%255 is not useful on Linux or Solaris. ret%256 might be, but
ret/256 is probably what you're looking for -- but only if the program
terminated normally. "man system" for details.
</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jan 19 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jose Fernandez | last post by:
Hello. I'm building a web service and I get this error. NEWS.News.CoverNews(string)': not all code paths return a value This is the WebMethod public SqlDataReader CoverNews(string Sport)...
2
by: Sam | last post by:
In our C++ program, we are using the system call to execute another C++ program synchronously. The program executed by system runs without error and returns back a 0. Under conditions we cannot...
3
by: Oberon | last post by:
How do I deal with this? I am getting an error for each get in the Game class (see code below). In the simplified example below I have reduced this to just 3 fields, one which can be NULL. I...
5
by: Dmitriy Lapshin [C# / .NET MVP] | last post by:
Hi all, I think the VB .NET compiler should at least issue a warning when a function does not return value. C# and C++ compilers treat this situation as an error and I believe this is the right...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
2
by: baret bonden | last post by:
Trying to return a selected listbox item to another form .tried lots of ways; defining public variables and passing those as well as textboxes ..I' m able to display the chosen item on it's form...
0
by: jeytu | last post by:
Hi all, I'm a newbie with .net. I have created a web method that would return a class. The definition of the class is <Serializable()> Public Class Response Dim l_status As Boolean ...
3
by: BombDrop | last post by:
Can any one help I have a method that will return a List to be bound as a datasource to a combobox see code for population below. I get the following error when i try to compile Error 29 ...
46
by: Bill Cunningham | last post by:
I have heard that return -1 is not portable. So what would be the answer to portability in this case? What about exit(0) and exit (-1) or exit (1)? Or would it be best to stick with C's macros,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.