what does int main() return and to what does it return
That is, in the code snippet,

int main()
{
 int a=0,b=5,c;
 clrscr();

 c=a+b;
 return c;
 printf("%d",c);

 getch();
}

1)What is the expected output ?
2)I tried compiling this and i dont get anything on the screen
3)In case it returns, then,
a)who is calling main ? (is it some systems call or something)
b)what is the value returned to ?

Edited 5 Years Ago by Narue: Added code tags

1)What is the expected output ?

Nothing. You return before printing any output.

2)I tried compiling this and i dont get anything on the screen

Yes, that's what I'd expect.

3)In case it returns, then,
a)who is calling main ? (is it some systems call or something)

The C runtime environment calls main. Often something like this:

exit(main(argc, argv));

b)what is the value returned to ?

The C runtime environment for subsequent processing. It may be ignored (as in the case of compilers that support void main, or it may be used in a system call returned to the OS (as in my example above).

Narue : Ma'am can we say that a return inside a main is used only to return program flow and NOT TO RETURN A VALUE ?

That is,
int main()
{
----
----
return;
}
is OK because it is used to return the program flow

WHEREAS
int main()
{
----
----
return c;
}
doesnt make any sense ?
because anyways the variable or whatever which receives the variable c, is incapable of performing any operations on it ?

can we say that a return inside a main is used only to return program flow and NOT TO RETURN A VALUE ?

No. A return statement with no value is the same as falling off the end of the function. It still returns, but if the function is defined to return non-void, it still returns a value, and that value will be indeterminate. The following two programs are functionally identical (and they both invoke undefined behavior):

int main(void)
{
    // No return statement
}
int main(void)
{
    // Empty return statement
    return;
}

Some programs spawn other programs, for example program A calls program B. In that case the return value of main() in program B can be caught by program A and used to determine if program B completed its task successfully or not. That's not the only reason, but just one of them.

A final clarification ma'am

U said that the function with return returns an un-predicatable value correct.


This is what StackOverflow has to say
"By convention, your program returns an integer value (the return value of main) to the shell to indicate whether the program completed successfully (a zero) or non-successfully (non-zero, often related to an error code)."
You may refer http://stackoverflow.com/questions/2263717/what-does-int-main-mean-in-c for the same

They say that a program which compiles correctly returns 0 in all cases.
So what about the un-predictable behaviour you were referring to ?

Ancient Dragon : Oh wow :) , nice , never thought about that.
So u mean to say that even a value returned by a main can be caught by say another program which spawned it and carry out operations... Nice Sir!!!

But in the event of a program not being spawned by other, ie in normal cases, who receives the value returned by main ? Is it the Shell (in order to determine whether the program has completed successfully or not) ?
This is what StackOverflow says
"By convention, your program returns an integer value (the return value of main) to the shell to indicate whether the program completed successfully (a zero) or non-successfully (non-zero, often related to an error code)."

They say that a program which compiles correctly returns 0 in all cases.

They didn't say that at all. You specify the return value with a return statement or by calling exit(), which is (by convention) 0 for success and non-zero for failure. Failing to return a value explicitly (see below for the exception) results in undefined behavior.

You might read somewhere that the latest C standard allows omission of the return statement from main and automatically returns 0 for success in such a case, but that's contingent on the code being compiled with a compiler in C99 mode. C99 isn't widely used, so we assume the previous C95 standard here.

Edited 5 Years Ago by Narue: n/a

Narue : Ma'am u said the C RunTime Environment calls main and also receives the value returned by main. Pleeez can u elaborate ?
I am beginning to understand. I would love to understand this completely

I dint understand what you meant by - exit(main(argc, argv));

It's not unreasonable to replace "C runtime environemnt" with "OS". The OS calls main and main returns to the OS. That's not strictly correct, but it's acceptable enough for understanding of the basic concept.

Ancient Dragon : Oh wow :) , nice , never thought about that.
So u mean to say that even a value returned by a main can be caught by say another program which spawned it and carry out operations... Nice Sir!!!

But in the event of a program not being spawned by other, ie in normal cases, who receives the value returned by main ? Is it the Shell (in order to determine whether the program has completed successfully or not) ?
This is what StackOverflow says
"By convention, your program returns an integer value (the return value of main) to the shell to indicate whether the program completed successfully (a zero) or non-successfully (non-zero, often related to an error code)."

When no other program or shell script has spawned the program then the os will most likely just ignore the return value. Afterall, the os doesn't really care whether the program completed successfully or not. And in some cases the return value doesn't mean success or failure at all, it could mean something else. 0 or 1 (sucess or failure) is the normal thing for main() to return, but there is nothing to prevent main() from returning other integer values.

This article has been dead for over six months. Start a new discussion instead.