-7

why do we need a return function in main in C? Because when I tried writing a statement after the return statement in main the statement after the main did not get executed but it should be executed after the control returns from the calling function back to the main it should execute the statement after the return statement till a closing bracket is encountered?

example:

int main()
 {
   printf("Hello World");
   return 0;
   printf("hi"); // This does not gets executed
 }
robsiemb
  • 4,959
  • 7
  • 24
  • 33
  • What? If you _return_ from main there is no calling function to return to (not in your code, at least). – tkausl Nov 09 '19 at 13:24
  • @PRIYANKA CHETTRI Show an example of a program. Otherwise it is unclear what you mean. – Vlad from Moscow Nov 09 '19 at 13:25
  • 2
    @PRIYANKA CHETTRI How the statement will be executed in main when the control is passed out of main due to the return statement? – Vlad from Moscow Nov 09 '19 at 13:29
  • The question in the title is not the same as the question you ask in the body. There are two different questions here - you should avoid that on SO - you may not get answers to both in one answer. – Clifford Nov 09 '19 at 14:05
  • Possible duplicate of [What should main() return in C and C++?](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Daniel Danielecki Nov 09 '19 at 18:39

3 Answers3

2

Why do we need a return function in main in C?

The return value from main() is returned to the "system" and interpreted as a process exit code which can be used in shell scripts and batch files for example. In Windows for example:

> myprogram
Hello World
> echo myprogram returned %errorlevel%
myprogram returned 0

[...] it should execute the statement after the return statement till a closing bracket is encountered?

No. A function returns when a return is encountered (clue is in the name!) or at the closing brace - whichever occurs first. A return statement can appear anywhere in a function and a function may have more than one return statement. If the closing brace is encountered before a return, then that is an implicit return which for a non-void function is undefined behaviour if the caller attempts to use the return value.

When you return from main() control is returned to the system, and in a hosted environment that terminates the process and the OS recovers resources, closes files etc.

Jonathan Leffler
  • 666,971
  • 126
  • 813
  • 1,185
Clifford
  • 76,825
  • 12
  • 79
  • 145
0

The 'return' statement is not a function call. It is actually the opposite. Instead of calling another function, so that a new function gets added to the call stack, it causes the current function to exit, as if the end of the function block had been reached. This causes the current function to get removed from the call stack. For this reason, any code written after an unconditional return statement is unreachable and will never get executed.

Functions with a return type of void (=nothing) do not require a return statement. There is an implicit return statement at the end of the function block that causes control to be returned to the calling function. In the case of the function main, the calling function (normally) belongs to the operating system.

However, if the return type is not void, then the function must return a value, so you require an explicit return statement. Since the C standard requires main to return an int, you have to explicitly return a value with a return statement, if your code is to be compliant with the standard. However, some compilers also accept void as a return value, although this is not part of the C standard.

Andreas Wenzel
  • 4,984
  • 2
  • 7
  • 24
  • 1
    It is not true that functions with a non-void return type must return a value. The C standard permits control to flow to the end of the function, and then control returns to the caller. (If the calling function attempts to use the return value, the behavior is undefined.) This is useful with functions that sometimes return a value and sometimes do not, such as a function that performs an operation specified by a request code, and some requests might set values and not return anything while others retrieve values and return them. – Eric Postpischil Nov 09 '19 at 14:05
  • 1
    Additionally, `main` is special; it does not have to have a `return` statement to return a value. If control reaches the end of `main`, it returns zero. – Eric Postpischil Nov 09 '19 at 14:06
  • @EricPostpischil: I distinctly remember my Microsoft compiler complaining when not all control paths of a function return a value. However, the compiler may have been in C++ mode (not C) and I'm not sure if that was a compiler error or a warning. Also, it may have been a very old version of the compiler. Are there any differences between C and C++ in this respect and have the standards possibly changed in this respect? – Andreas Wenzel Nov 09 '19 at 14:18
  • 1
    A compiler might warn, as this use is rare, but it should permit it when compiling for standard C. Different compiler switches could affect that. In C++, the behavior is undefined: C++ 2017 draft n4659 9.6.3 “The `return` statement” [stmt.return] 2 says “… Otherwise, flowing off the end of a function other than main (6.6.1) results in undefined behavior.” (The “Otherwise” follows a sentence about functions with void return types, so this sentence is about functions with non-void return types.) – Eric Postpischil Nov 09 '19 at 14:41
0

return is a keyword not a function. It returns from the currently executing function to the caller.

The main function is called from the startup code, and usually the return value contains the exit status from the program.

Eric Postpischil
  • 141,624
  • 10
  • 138
  • 247
0___________
  • 34,740
  • 4
  • 19
  • 48