Just a Question on C programming basic some instructors have different ways of using either getche() or return 0; as bottom code on main function. Which is more better to use getche() or return 0;?

Getche() vs return 0;

Recommended Answers

All 6 Replies

getche() is a non-standard function and should not be used at all. They are trying to keep the program window open when you execute the code using the IDE. It's best to use a standard C function (like getchar() ) for that. return 0; is required because main() is an integer function. Any instructor that doesn't use return 0; is probably using void main() , which is wrong. Try not to take classes from these instructors -- they don't know the language well enough.

Thanks that enlightens me, so when do you use return 1 to ....; or other variables or is the return code also accepts other variables? C programming is really not getting too much on my head

Use return 1; to indicate an error. return 0; indicates a successful execution.

All other values are not portable and may have no effect on many systems. I don't know if they'd be a problem, though.

On Windows, other values can be seen by the system and used. A batch file can get the exit status and process different code based on the value.

getche() is a non-standard function and should not be used at all. They are trying to keep the program window open when you execute the code using the IDE. It's best to use a standard C function (like getchar() ) for that. return 0; is required because main() is an integer function. Any instructor that doesn't use return 0; is probably using void main() , which is wrong. Try not to take classes from these instructors -- they don't know the language well enough.

The points above are so interesting....
Can I know why is it wrong to use void main().
What happens if I use like that.I'm asking since my programs with void main() are also working fine.
Thank you

Because by definition and design of the language it is wrong. See this.

You must understand that just because something works fine does not mean it's correct. It is very possible to drive through a red light. It works. But various bad things could happen. And because the law says don't do it we don't do it.

Firstly, void main() is non-standard. Just because it works using your current compiler on your current operating system in your current hardware is no guarantee that it will work elsewhere. Deliberately writing programs like this is foolish when you could just write int instead of void.

Here is a page discussing it, http://c-faq.com/~scs/readings/voidmain.960823.html , of which the last lines are most relevant:

Besides the problems of a garbage exit status being received by the calling environment, a more serious failure could occur if the compiler used different calling conventions for void- and int-valued functions. If so, then for main() to push no return value on the stack, and for the caller to pop an int value that main() hadn't pushed, could conceivably "screw later uses of the stack up enough that the program could crash."

Here's more: http://users.aber.ac.uk/auj/voidmain.shtml

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.