Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I searched on google but I could not get the practically right answer for this question..
My program is :-
C++
#include<stdio.h>
#include<conio.h>
int main()
{
	int temp;
	clrscr();
	printf("Enter Number : ");
	scanf("%d",&temp);
	return temp;
}


How do i get the temp variable's value by main function(at runtime)??
I'm using Dos Box C IDE, and windows 7(64bit).

thanks in advanced...
Posted
Comments
Prasad Khandekar 17-Jun-13 10:23am    
Do you mean to get the return value in a batch file?
JayantaChatterjee 17-Jun-13 10:28am    
yes!!
if there were another way to do that, then give that also..
Prasad Khandekar 17-Jun-13 10:43am    
Hello jayanta,

Call your executable from batch file and immediately after that check value using %errorlevel%. Something like

@echo off
YOUR.EXE
SET /a retVar=%errorlevel%
ECHO %retVar%


Regards,
JayantaChatterjee 17-Jun-13 10:51am    
Thanks a Lotttttttttttttt..
Its works....
Thanks again...

1 solution

This return value is the value which a parent process can get. You got an answer on how to get this value in a batch script, but any other process can get it. This return value is called exit status. You can read about it here:
http://en.wikipedia.org/wiki/Main_function[^],
http://en.wikipedia.org/wiki/Exit_status[^].

The returned value does not have special predefined meaning and is fully defined by the application, but zero is usually used to conduct the "complete without any problems" idea. The way of getting this value depends on the platform, but multiplatform programming systems/language may have some common API for spawning a child process, and, in such cases, the API usually provides a way to read exit code.

With time, the use of such a weakly defined entity as exit code somewhat declined. Many application, especially those with GUI majorly ignore the value, always returning 0. In console-only applications, using it is more usual.

—SA
 
Share this answer
 
v2
Comments
CPallini 17-Jun-13 11:27am    
My 5.
Of course you might get such value from command prompt either.
Sergey Alexandrovich Kryukov 17-Jun-13 11:55am    
Thank you, Carlo.
As to the "command prompt", this is nothing more than one of possible parent processes, a command interpreter and processor. Any process can spawn another process and obtain the exit code value exactly the same way the "command prompt" does.
—SA
CPallini 17-Jun-13 12:14pm    
OK, but the role of the command prompt is, from the user point of view, special, because the user himself can directly inspect the value.
Sergey Alexandrovich Kryukov 17-Jun-13 12:53pm    
Well, sure.
—SA
Prasad Khandekar 17-Jun-13 11:29am    
My 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900