Click here to Skip to main content
15,902,198 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It's been ages and I'm yet to understand return types and parameters completely in C.
int main()
void main()
void main(void) ....etc

What do these declarations actually mean? I read that that the compiler returns 1 or 0 to the operating system.
What does the compiler have to do with the operating system?
Why do we pass parameters to a function after all? Can't we declare whatever we need inside that function?
A clear and concise answer is most appreciated ....
Posted

1. main() will return an int to the O/S when it finishes
2. main() will return no value to the O/S (no longer valid C)
3. main() takes no variables and returns nothing to the O/S

4. The compiler returns a value that indicates success or error. Batch files and ShellScripts can base decisions on the value returned to the OS by a program. E.g - if the compile phase is unsuccessful, there's no point trying to link it.

5. Because any useful function can operate on different data in the same way. Take for instance printf - you pass it a string that includes text and formatting info, in addition to the data you'd like printed in a formatted manner.

Or take for instance a multiplication function - it's only useful if you can tell it what numbers to multiply (input variables) and can receive the result of the calculation (return value)
 
Share this answer
 
If you're using a default lib compiler option, then your code will actually have additional code that calls main and handles memory allocation, preparation, security cookies, and etc. When that ends, after you've returned from main, the OS will get a status from the program, that status being whatever you return from main.

In windows, GetExitCodeProcess and %errorlevel% are ways of seeing this.

tl;dr you can notify the user and OS if your app did not complete correctly.
 
Share this answer
 

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