By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,631 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

hi i am a computer science graduate.....

P: 17
hi i wana know about the return type of main( ) in c.
where is it returned ... and can we print the value anyhow.,
Feb 20 '07 #1
Share this Question
Share on Google+
15 Replies


Frinavale
Expert Mod 5K+
P: 9,731
hi i wana know about the return type of main( ) in c.
where is it returned ... and can we print the value anyhow.,
Hi there,

The return value is returned to the Operating System when your program finishes.
It is used to indicate if something went wrong.

This return value is very useful for stuff like Bash Scripting in Linux/Unix where you call on a program to do something and depending on whether or not it fails do something.

Welcome to the scripts!

-Frinny
Feb 20 '07 #2

P: 4
To print the value which was returned to Operating System, After u execution of the program u need to give "$?" command at shell.. it will show the value returned to OS.
Feb 21 '07 #3

RedSon
Expert 5K+
P: 5,000
hi i wana know about the return type of main( ) in c.
where is it returned ... and can we print the value anyhow.,
When did you graduate?
Feb 21 '07 #4

P: 1
hi i wana know about the return type of main( ) in c.
where is it returned ... and can we print the value anyhow.,
Hi,
The main() can return any type of value you want like. It returns it value to the command prompt from where generally u execute the .exe file. The main() generally carry two types of argument one of which is numeric and otherone is character type.
Feb 21 '07 #5

Motoma
Expert 2.5K+
P: 3,235
hi i wana know about the return type of main( ) in c.
where is it returned ... and can we print the value anyhow.,
The return value is given to whatever program started the executable. In shell scripting it would be the script, if you called it from inside another program, the program would recieve the returned value at the end of execution.
You can use this in scripting and programming to determin the execution flow of the program without having to do STDOUT redirection and analysis, as well as determing the final condition of the program.
Feb 21 '07 #6

P: 17
plz guide me ...
i am coding in turbo c.
i wrote command "$?" at dos prompt...
but it doesnt recognize the command.. and plz send the details.
i hve to print the return type of main anyhow..
thnks
Feb 23 '07 #7

Motoma
Expert 2.5K+
P: 3,235
In Windows, the variable is errorlevel, not $?.
You can use it on the command line as %errorlevel%, and in batch scripts as errorlevel.
Feb 23 '07 #8

P: 26
can you compar the main and the function
Feb 23 '07 #9

P: 26
mr. majidmajid



if you are very new to program then take tot and type one program and try to understand the statment and try to impliment it by your own. then you get nice kwl.
Feb 23 '07 #10

P: 26
mr.majidmajid
display your code

it will be easy to guid you
Feb 23 '07 #11

P: 40
question: what's the difference between return(0) and return value?
Feb 23 '07 #12

Motoma
Expert 2.5K+
P: 3,235
question: what's the difference between return(0) and return value?
Example:
Program A runs. It does a return 0 for success, a return 1 for "invalid filename", and returns 2 for "invalid username"
Program B launches Program A:
Expand|Select|Wrap|Line Numbers
  1. int returnValue = exec("progA");
  2. switch(returnValue)
  3. {
  4.   case 2:
  5.      // invalid username
  6.   case 1:
  7.      // invalid filename
  8.   case 0:
  9.      // returns correctly
  10. }
  11.  
Does this make sense to you?
Feb 23 '07 #13

P: 26
y we want to return the value for function
Feb 25 '07 #14

Frinavale
Expert Mod 5K+
P: 9,731
y we want to return the value for function
The main()'s return value is used by the calling program to determine how the program ended. This could be your operating system or it could be another script or program.

Example:

Program 1
Say we create a program which returns "1" in the case where it encountered an internal error and couldn't complete its processing...and in the case where it doesn't encounter an error and it's able to complete, we return "0".

Program 2
Say we create another program that calls our first program and displays an error message if our first program crashed and displays a "thank you" message if our first program didn't crash.

This program would use the value returned by your main() to determine which message to display.

Does this make sense?

-Frinny
Feb 26 '07 #15

RedSon
Expert 5K+
P: 5,000
Example:
Program A runs. It does a return 0 for success, a return 1 for "invalid filename", and returns 2 for "invalid username"
Program B launches Program A:
Expand|Select|Wrap|Line Numbers
  1. int returnValue = exec("progA");
  2. switch(returnValue)
  3. {
  4.   case 2:
  5.      // invalid username
  6.   case 1:
  7.      // invalid filename
  8.   case 0:
  9.      // returns correctly
  10. }
  11.  
Does this make sense to you?
Don't forget your break; s or you will be returning correctly every time.
Feb 26 '07 #16

Post your reply

Sign in to post your reply or Sign up for a free account.