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; 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; Short answer: prefer standard C semantics (use an int-returning main and the standard input functions) and treat getch/getche as platform-specific hacks for old DOS/Windows toolchains. As pointed out, getch/getche come from the nonstandard conio.h family and are not part of ISO C; Microsoft documents its own variants (e.g., _getche) but they’re Windows-specific. Microsoft _getche docs (learn.microsoft.com) conio.h (non‑standard). (en.wikipedia.org)
About main and return values: the hosted C standard expects main to return int; using void main is non‑standard and can lead to undefined or implementation‑dependent behavior as other posters warned. Since C99 the standard says that falling off the end of an int main is equivalent to returning successful status (zero), but that historical differences exist before C99, so write the conventional signature (int main(void) or int main(int argc, char *argv[])) and return a status explicitly or use the standard macros for clarity. C99 program‑termination text (draft) (landley.net)
Exit codes and portability: return 0 means success; non‑zero means failure. For readable, portable code prefer the macros EXIT_SUCCESS and EXIT_FAILURE from <stdlib.h> rather than hard‑coding numbers; hosts and shells interpret non‑zero values differently, and some systems truncate or reserve ranges for special meanings. EXIT_SUCCESS / EXIT_FAILURE reference. (en.cppreference.com)
If the only goal is “keep the console open” while learning, do that in the IDE (run in terminal / pause-on-exit setting) or run the program from a real shell. Avoid system("pause") for portability; use a standard, documented input call to wait for input if needed (standard input reads are portable). getchar / stdio reference — and note the Windows PAUSE command is a shell built‑in, not a portable solution. (en.cppreference.com)
Bottom line: follow and — write int main(...), use standard library routines for portability, and reserve Windows‑only functions (getch/getche, system("pause")) for quick, platform‑specific experiments only.
Jump to Post— WaltP 2,905
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 (likegetchar()) for that.return 0;is required becausemain()is …
Jump to Post— WaltP 2,905Use
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. …
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 (likegetchar()) for that.return 0;is required becausemain()is an integer function. Any instructor that doesn't usereturn 0;is probably usingvoid 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, , 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:
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.