Yesterday in the interview one question was asked to me that can main
return struct
?
I have no idea can any one please tell me is it possible or not,if yes why?

- 39,972
- 7
- 52
- 94

- 1,116
- 3
- 13
- 18
-
7why do I never get questions as simple as this one when I do get interviews? :) – zmo Jun 23 '13 at 16:20
7 Answers
main
can only return an int
value in C (at least for hosted implementations).

- 142,963
- 15
- 272
- 331
-
-
4`exit` and return from main are not quite equivalent. If you call `exit`, the `atexit` handlers can access automatic-storage-duration objects from the initial invocation of the `main` function and any other function invocations that have not yet returned. If you `return` from `main`, the lifetime of these objects has already ended and you cannot access from from `atexit` handlers. Also, if you've called `setbuf` or `setvbuf` on any streams with a buffer having automatic storage duration, returning from `main` invokes UB but calling `exit` does not. – R.. GitHub STOP HELPING ICE Jun 23 '13 at 16:41
-
@R.. +1 for interesting points, but then why does standard explicitly say that they are equivalent? (5.1.2.2.3/1) – effeffe Jun 23 '13 at 17:31
-
2It doesn't say calling exit is equivalent to returning from main. It says that returning from main is equivalent to calling exit *with the value returned* from main. Note the word "returned" implies that the return has already taken place at the time the action equivalent to a call to `exit` takes place. And as always, one of the effects of `return` is that the lifetime of automatic objects ends. – R.. GitHub STOP HELPING ICE Jun 23 '13 at 18:32
-
@R.. The shortcut in my comment was too short, thanks for the corrections. – ouah Jun 24 '13 at 13:48
Section 5.1.2.2.1 of the C standard says no:
The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[]) { /* ... */ }
or equivalent;) or in some other implementation-defined manner

- 14,395
- 10
- 44
- 68
-
-
-
Hm, that's possibile, but it's not so clear (well, at least for a non-English man like me). Anyway, the standard explicitly talk about different return types for `main`, saying that the behavior is not undefined. – effeffe Jun 23 '13 at 17:43
-
An implementation may define additional signatures for
main
, but it must document those additional signatures. If the documentation doesn't list it, then it isn't supported. – John Bode Jun 23 '13 at 23:33
No you can not
If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument;10) reaching the } that terminates the main function returns a value of 0. If the return type is not compatible with int, the termination status returned to the host environment is unspecified.
And you can not pass a struct to main, so the best is write this struct to a binary file. What do you wanna do?
Edit (for @effeffe):
#include <stdio.h>
struct st{float f;};
int main(void)
{
struct st x;
x.f = 3.14;
return x;
}
Returns
demo.c: In function ‘main’:
demo.c:9:5: error: incompatible types when returning type ‘struct st’ but ‘int’ was expected

- 39,972
- 7
- 52
- 94
-
Where does that quote say that we can' do it? Doesn't it just say that, in such a situation, we wouldn't know the returned status? (which is not a good thing of course) – effeffe Jun 23 '13 at 18:02
-
If the return type is not compatible with int, the termination status returned to the host environment is unspecified. – David Ranieri Jun 23 '13 at 18:15
-
-
I don't understand, what has this to do with the question? That code is trying to return a value of a type that it's incompatible with the declared return type of the function, and this is always wrong. The standard you quoted says *If the return type is not compatible with `int`...*, not *If the value of the returned type is not compatible with `int`...*. It's talkin' about the declared return type of the function, not the actual type of the value in the `return` statement. – effeffe Jun 23 '13 at 19:04
-
ohhhh, yes, now I understand you, (the quoted text is talking about the declaration in `int main`, not about the return value), YOU'RE RIGHT!! – David Ranieri Jun 23 '13 at 19:22
main
can only return int
in C/C++. This has to do with the ability to return a status code when a program has completed. 0
means success while 1
through 255
meant a particular error. Program would come would some sort manual in a text file or in print that would have the meaning of those errors so you could determine why your program couldn't run. This is assuming someone went through the effort of error checking and produced documentation.

- 1,417
- 5
- 28
- 57
Since main is originally called from the operating system's program loader, it would have no idea what you're returning or what's supposed to be done with it.
Therefore, the convention is: return an integer. No matter what language, or what program, this provides a common interface for programs to communicate with the shell that launched it. This also means non-technical users who write scripts can use this integer to make decisions within their scripts.
The simplest way you can perform what you're asking is to save the structure to a file. This way any program that is aware of your structure's data can access the file.
-
Main is not called from the OS program loader. Actually it's not even the first code executed after launching your program. Read more here http://stackoverflow.com/questions/4783404/is-main-really-start-of-a-c-program – sasha.sochka Jun 23 '13 at 16:45
-
I wasn't writing a paper on OS structure. When a person asks if they can return a structure from main I assume they're not highly technical. Oh and BTW, yes it is. What else could get main executed? The print spooler? – Jun 23 '13 at 16:52
A joke answer:
A lot of people say no, but I think yes! (Sort of)
You could return a 32 bit pointer, which you then cast to an int, which points to a struct on the heap, assuming that int is of size 32 bits, and you have a 32 bit system so that the pointer is of size 32 bits also.
Then, rather dangerously, hope the operating system doesn't allocate that memory to another program to use, and hope that program doesn't overwrite it. If you use an os like Windows or Linux then my understanding is even if you don't free the memory, then the os will free it for you anyway. (Although you should always free your memory of course, else you be a dirty programmer!)
As far as I am aware another program could then declare a pointer to the location returned by your main(), and then read the contents of what was in the (now gone) struct byte by byte.
I think this should work? Time to test it! (This may go horribly wrong and lots of undefined things may happen.)
Don't take this answer too seriously. You will probably ruin your employment chances if you suggest it.

- 13,167
- 27
- 115
- 225
As others have noted, if the return value of main
is not a type compatible with int
, its value is "unspecified". That means that a compiler could do just about anything it likes with the return value without violating the C standard, and it is not possible for a portable C program to return a structure type. On the other hand, nothing in the C standard requires that compiler vendors not document what will happen if code tries to have main
return a struct type, nor does it require that the compiler's documented behavior must never be useful. If an OS had a primaryReturnCode
and secondaryReturnCode
variables, and a compiler vendor wanted to specify that returning a struct containing two int
values would cause the first value to be stored in primaryReturnCode
and the second in secondaryReturnCode
, such a specification would be perfectly legitimate.
In C++, a program is required to have a main
function that returns int
. It would also be possible to have other functions with the name main
which return other things. I don't know whether a function which was named main
but had different parameters and return type from the startup main
function could appear in the same context as the startup one, but if nothing else functions called main
should be allowable within class scope.

- 77,689
- 9
- 166
- 211