main's return value is of type int.
I've read that the value is returned to the OS(meaning?).
Is there any possibility of using this value in a C program?
I have'nt come across any feature in C which does....
Printable View
main's return value is of type int.
I've read that the value is returned to the OS(meaning?).
Is there any possibility of using this value in a C program?
I have'nt come across any feature in C which does....
The return value is passed back to the invoker when the program terminates. It's nice to tell the invoker how you went.
system() for example might use such a value: http://www.cplusplus.com/reference/c...ib/system.html
and you might want to write a little math program or something, eg:
Which pointlessly adds 2 numbers like so:Code:#include <stdlib.h>
int main(int argc, char* argv[])
{
int res = 0;
if(argc >= 3)
res = atoi(argv[1]) + atoi(argv[2]);
return res;
}
./pointless.exe 5 10
= 15
When you write some shell script or bat-file calling external program you'll want to add some branching depending on the success/failure of the action you just executed...Quote:
I've read that the value is returned to the OS(meaning?).
That's why in the batch file you can exemine the return code of the program and use it in the if conditions...
Suppose i do
The parameter rv will hold 0 if system() has executed correctly. But, one of his questions were :Code:int rv;
rv=system("./hello");
"s there any possibility of using this value in a C program?"
But, "rv" is not actually the return value of my "hello" program., right? And something like this is easily done in shell scritps as vart suggested. Can we do it in C?
rv can indeed be the return value of the program. Whether it is or not depends upon other factors, such as whether or not the command you ran was actually able to be executed or not.
I wrote a post on the return value of main(), with regard to why it is important to declare main() as returning an int, and to return an int that properly reflects the state of the program when it reliquishes control and terminates. This is somewhat intermediate to advanced, but you can try the programs I have in the post. They may not work, since they are mainly proof-of-concept programs. As you can see, I used the return value of system() to read the return value of the command being executed, which can indeed be an actual C program:
http://cboard.cprogramming.com/showp...0&postcount=11
If we use some advanced API like CreateProcess in Windows we can afterwards retreive the exit code of the process using corresponding function http://msdn2.microsoft.com/en-us/library/ms683189.aspx
There's also a POSIX function called spawn*() (spawnl, spawnp, spawnv etc, depending on what you want) that allows you to get the return value of a process. Just use mode P_WAIT.
Note that (despite what many people think) the requirement for main() to return int only applies to what the C standard called "hosted environments," which usually means that your program is executed by an operating system.
In non-hosted environments, typically embedded systems, the name, return type, and parameters of the "main" function are implementation defined.
I've done some embedded software in which main() was required to return void, which is perfectly legal in those cases. (C99 5.1.2.1, in case anyone is interested)
hi all!
The replies have been interesting.
From all of them i could gather the following.
Correct me if I am wrong.
Return value of main is passed back to the invoker after the
progam terminates so i believe OS invokes the program (in an ordinary case)
and finally the value is returned to it.Fine!
As to my question where in OS i take it that it is stored in EAX register
according to MacGyver.
Now the second part of the question was whether it can be used in another
C program.
If i can put my question in a slightly more blunt way
For example two program P1 and P2
Code:P1
int main()
{
int a;
...
...
a=5;
return 5;
}
P2
{
..
...
if(b>a)// a is the one from P1
...
}
1.
@zacs7
Your program returns 15 ok! and it may be utilized by 'system' function
but how to retain it??
2.
@vart 1st post
In response to my first part(i.e value returned to OS) your reply was how
the value could be utilized and also you suggested examining the batch file
to retrieve the value(isnt it?)
@vart 2nd post and @dwks
I am not very familiar with API's so excuse me if i get this wrong.
What you have suggested is the following i guess
1.Read return value of P1 using certain functions or procedures in API
like GetExitCodeProcess and spawn*().
2.From API retrieve the value for use into P2(??)
A register is probably not what you think it is. It's one of very few memory locations inside the CPU. But that doesn't really matter.Quote:
As to my question where in OS i take it that it is stored in EAX register
according to MacGyver.
Yes, the return value from one program can be used in another program.Quote:
Now the second part of the question was whether it can be used in another
C program.
Does that answer your question? Well, yes, but your question should have been, how? :)
What operating system are you using? Windows or Linux? If you're using Windows, execute the program with CreateProcess() (a link was posted to it by vart above). If you're using some other system like Linux or UNIX, check out the last post in this thread. http://www.programmersheaven.com/mb/...admessage.aspx
[edit] You posted again while I was typing . . . okay:
What I mean is this:Quote:
@vart 2nd post and @dwks
I am not very familiar with API's so excuse me if i get this wrong.
What you have suggested is the following i guess
1.Read return value of P1 using certain functions or procedures in API
like GetExitCodeProcess and spawn*().
2.From API retrieve the value for use into P2(??)
Code:/* executor */
#include <stdio.h>
#include <stdlib.h>
#include <process.h> /* I think this is what spawnl() is in . . . */
int main() {
int retval = spawnl(P_WAIT, "executee.exe", NULL);
printf("executor: %d\n", retval);
return 0;
}
[/edit]Code:/* executee */
#include <stdio.h>
int main() {
int r;
printf("Enter the value to return: ");
scanf("%d", &r);
return r;
}
@MacGyver
Code:int iRet;
char szProgram[256];
while(getProgram(szProgram,sizeof(szProgram)))
{
iRet = system(szProgram);// HERE THE RETURN VALUE MAY BE ANYTHING !!
printf("Return value of \"%s\":\t%d\n\n",szProgram,iRet);
}
Your idea is interesting getting the value from EAX register,
but i think the part where you try to do
iRet=system(szProgram);
the RETURN VALUE OF THE SYSTEM NEED NOT BE THE RETURN VALUE OF PROGRAM.This was according to the link given by zacs7.
I also ran your 3rd program and i was getting the output 32512 on passing
the name of any file.
http://www.cplusplus.com/reference/c...ib/system.html
Any suggestions?
I'd better start a new post.
Yes. Here's an example sh script (I'm not sure about the batch syntax):Quote:
2.
@vart 1st post
In response to my first part(i.e value returned to OS) your reply was how
the value could be utilized and also you suggested examining the batch file
to retrieve the value(isnt it?)
Note that I rarely use shell scripting, so that might be wrong, but it's probably irrelevant since you want to do this in C, not sh.Code:#!/bin/sh
VALUE=$(program.exe)
if [ $VALUE -gt 0 ]; then
echo "Error encountered (return value of program.exe was more than 0)"
fi
[edit] Arrg, you beat me again.
Yes, the return value of system() is not the return value of the program. To get the return value of the program, use some other function like that Windows one or spawnl(). What OS are you using? [/edit]
@dwks
It is the right header file I have to still check out
your shellscript method..
@dwks
oops!
sorry for posts in between :) did'nt check your
posts..
I am currently running my programs on a LINUX platform.
I just ran your program.The header file "process.h" was not available..
The FAQ has a detailed description of spawn() and family, and it uses process.h. http://faq.cprogramming.com/cgi-bin/...&id=1043284392
Try searching your header files, I'm sure that one of them has "spawn". I think that some systems use posix_spawn_something().
The Wikipedia entry has lots of information about spawn(): http://en.wikipedia.org/wiki/Spawn_%28computer%29
http://en.wikipedia.org/wiki/Process.h seems to suggest that process.h is a windows specific header.
Indeed, quite correct.
This was also given in my post in this topic:
This matches what you discovered:
I found this having to do with ruby, but I find the return value interesting. More googling indicates that the value 32512 is a value that can be obtained from system().
I did warn that the programs I provided are proof of concept, and while I would encourage people to experiment, to be prepared for this. They naturally worked for me, but I have little confidence in system()'s portability in this regard since it relies heavily on the behavior of O/S-specific shells, and I admit I know not much about the underlying mechanisms for most systems.