# About only one function (C programming)

• C/++/#
I have learnt that in c programming language at least one function is needed. And the function is given as:

int main(){

//statements;
return 0;
}

Then when I turned to the function page (in a web tutorial) I saw functions are declared by return type-function name and parameters.

The questions are:

1) Should the only one function be main()? Why not writing any other name than main? Is it possible?

2) Why return type is integer here? Specially as a beginner the programming I am doing is printing, or sum, making patterns with stars, loops type.. So, why return type not void, but int? And why we are saying return 0?

QuantumQuest
Gold Member
Hello fireflies

About your first question: In order for your program to run, main() function is needed. "Main" is used by convention. No other name can be used there.
About second question: The return type is integer because this is returned to the operating system in order to be marked the successful (or not) run of your program. Returning "0" means success.

Orodruin
Staff Emeritus
Homework Helper
Gold Member
2021 Award
The int main() function is the function the compiler looks for when it wants to know where your program should start executing the code.

The return type is defined as int. The returned integer will tell the system if your program executed as it should.

So what if we don't use the return 0; statement? Then it shows some other values(are they garbage values or where they come from?) Can we write anything else than return 0, like return 1? Then is it like that it would return 1 when executed?

Mark44
Mentor
So what if we don't use the return 0; statement? Then it shows some other values(are they garbage values or where they come from?) Can we write anything else than return 0, like return 1? Then is it like that it would return 1 when executed?
You can return whatever value you want, but this value would be used only by the operating system. If you had a batch file that started your program, the batch file could take different actions, depending on the value returned by main().

If you don't have a return x; statement in main(), I believe the compiler will issue a warning.

So yes, you must have a 'main' function because a C compiler expects there to be one.
However the function can be completely empty.

int main(){
}

in which case the 'main' function (the entire program!), successfully achieves doing nothing.

Last edited:
jim mcnamara
Mentor
Nothing beats a simple example
Code:
// Here is a simple student written complete program - with two functions: main() calls power() and then prints the returned value the returned value
#include "min.h"
double power(double x, double tmp, int n)
{
tmp*=x;
return (n>1)?power(x,tmp,n-1): tmp;
}

int main(int argc, char **argv)
{
double tmp=1.;
printf("%f\n",power(2, tmp, (argc>1)? atof(argv[1]): 4 ) );
return 0;
}

Code has issues so do not use it for anything but amusement. It was the first short C program I could find.

chiro
You can choose another entry point function but you have to tell the compiler what it is.

It's just convention to use main, or WinMain or some other function based on the language, operating system/platform and other possible factors.

The integer expression is mostly for the operating system which uses it to determine how the program executed.

fireflies
I have learnt that in c programming language at least one function is needed. And the function is given as:

int main(){

//statements;
return 0;
}

Then when I turned to the function page (in a web tutorial) I saw functions are declared by return type-function name and parameters.

The questions are:

1) Should the only one function be main()? Why not writing any other name than main? Is it possible?

The program in fact does not start by executing main(). There are some setup code, and cleanup/exit code (which is added by linker). And somewhere in the setup code, there is a statement "exitcode = main(argc, argv);"

If your program has no main(), that statement can't be successfully linked: "main" is not defined. Therefore linking will fail and executable won't be created.

Last edited:
Staff Emeritus
2021 Award
The integer expression is mostly for the operating system which uses it to determine how the program executed.

I always wondered why this was an int and not an unsigned int.

jbunniii
Homework Helper
Gold Member
I always wondered why this was an int and not an unsigned int.
Unsigned ints were not in the earliest versions of C. They were introduced in K&R C, circa 1978 (WIki). Changing the return type of main would have broken every program in existence at that point. (All five of them. ) So, as with many odd features of C, C++, and related languages, we're still paying the price for decisions that were made when C was still essentially a toy project among a handful of researchers.

Mark44
Mentor
Unsigned ints were not in the earliest versions of C.
I was thinking that this might have been the case, so I checked in my K & R, 1st Ed., and they mentioned signed and unsigned int types, so as you say, this was a decision made prior to the first K & R book.
Thanks for digging this up.

D H
Staff Emeritus
Changing the return type of main would have broken every program in existence at that point. (All five of them. )
Maintaining backwards compatibility is important!

So, as with many odd features of C, C++, and related languages, we're still paying the price for decisions that were made when C was still essentially a toy project among a handful of researchers.
Backwards compatibility is also the one of the key reasons every software project eventually turns into a big ball of mud.

D H
Staff Emeritus
Look at the price Python paid for not maintaining backwards compatibility. The initial plan was that maintenance of Python2 would have stopped somewhere in 2015. Python3 was so cool that everyone would obviously have gladly jump on board.

Now it's 2020 (and I suspect it will be later) when Python2 support will be suspended because yet another big ball of mud (*cough* Linux *cough*) cannot make the switch to Python3. So now Python has two big balls of mud to push around. When something nasty happens (e.g., Heartbleed), people have to be pulled off the newer, cooler ball of mud to patch the older ball of mud on which the Linux ball of mud relies so heavily. Python is yet another mature language, with all the baggage that that entails.

Full disclosure: I only jumped on board Python3 a couple of years ago. I, too, was attached to that old ball of mud. I did not have time to fix umpteen lines of code that worked quite nicely in Python2 but would have blown up if executed under Python3.

Pepper Mint
rcgldr
Homework Helper
I always wondered why this was an int and not an unsigned int.
Could be due to a very old version of C, or it could be due to the fact that for most operating systems back then and still to this day, the exit code is limited to 0 to 255, which could be stored in an unsigned characters. In the case of MSDOS, the standard exit call sets AH to 0x4C and AL to the exit code, and calls INT 0x021 (in assembly an "h" suffix is used instead so it's AH = 04Ch, AL = exit code, INT 21h).

Another thing that K&R later admitted they got wrong was setting the mathematical & and ^ operators to be lower precedence than the logical operators. They considered fixing this in the second edition, but didn't want to break existing code, although it's doubtful than anyone would have relied on this backwards precedence. In contrast, a mathematical language from the 1960's called APL, had a couple of minor issues similar to this, but these were fixed (also back in the 1960's), rather than let them permeate into the future.

Ah yes, ye olde terminate program interupt.
Shortest possible machine code program - just call int 21.
You can't do that in windows or android apps because it's sort of considered immoral.
Your device might reboot if you are lucky

Last edited:
Mark44
Mentor
Ah yes, ye olde terminate program interupt.
Shortest possible machine code program - just call int 21.
With AH set to 0. Also, int 20h terminates the program. Both of these are operationally equivalent, so sayeth my "DOS and BIOS Functions Quick Reference," QUE.
rootone said:
You can't do that in windows or android apps because it's sort of considered immoral.
Your device might reboot if you are lucky
Since Windows 95 (and even before, in Windows 3.11, Windows for Workgroups, I believe), the DOS functions (the int 21h interrupts) aren't supported, although I believe they were supported in the 16-bit virtual DOS machines on these OSes. I remember writing a bunch of short x86 assembly routines in the early 90s, and after Win 95, they wouldn't run any more.

Pepper Mint
jtbell
Mentor
Shortest possible machine code program - just call int 21.
You can't do that in windows or android apps because it's sort of considered immoral.
Not to mention classic Mac OS, Unix on various systems, etc.
"int 21? Eh, wuzzat? "

The integer expression is mostly for the operating system which uses it to determine how the program executed.

Unsigned ints were not in the earliest versions of C. They were introduced in K&R C, circa 1978 (WIki). Changing the return type of main would have broken every program in existence at that point. (All five of them. )

But if we write just main() instead of int main() then still the code runs?

jbunniii
Homework Helper
Gold Member
But if we write just main() instead of int main() then still the code runs?
Traditional C, up to and including the 1990 standard, allowed functions to be declared/defined without an explicit return type, in which case the implicit return type is int. This was disallowed starting with the 1999 standard (and C++ never allowed it, as far as I know), but many existing C compilers (including, notably, the one shipped with Microsoft's Visual Studio) have not been and probably never will be updated to support C99.

Also, of course, many compilers such as gcc allow behavior that is not technically legal according to the C standard. E.g., many compilers allow the return type of main to be void.

fireflies
jtbell
Mentor
But if we write just main() instead of int main() then still the code runs?
Try it with your compiler and see what happens. If it runs, and you really insist on saving four keystrokes, and the code is only for your own personal use, then go ahead and do it. But if you switch to another compiler that doesn't accept it, don't complain about it, just fix it. Or if you give your code to somebody else and he complains about it because it doesn't work on his compiler, just apologize and don't argue with him.

D H
Staff Emeritus
But if we write just main() instead of int main() then still the code runs?
That depends on your compiler and the settings.

Before the C was officially standardized in 1989, there was an unofficial standard called K&R C, short for the book "The C Programming Language" by Brian Kernighan and Dennis Ritchie. In that old-style C, omitting the return type from a function implicitly meant the function returns an int. Omitting the return type from a function has never meant the function returns void. Using void main() was viewed as an atrocity from day one.

There's a lot of very old code out there that is still running, so lots of compilers still accept that style, with coercion. This is the 21st century. You should not write code like that. Leave that code in the previous millennium where it belongs.

fireflies
QuantumQuest
Gold Member
But if we write just main() instead of int main() then still the code runs?

main() is a bit old. It is accepted in C89. It is no longer allowed (C99). But its return type although not specified, defaults to int

fireflies
Also, of course, many compilers such as gcc allow behavior that is not technically legal according to the C standard. E.g., many compilers allow the return type of main to be void.

Did you mean return type cannot be 'void' anymore?

Try it with your compiler and see what happens. If it runs, and you really insist on saving four keystrokes, and the code is only for your own personal use, then go ahead and do it. But if you switch to another compiler that doesn't accept it, don't complain about it, just fix it. Or if you give your code to somebody else and he complains about it because it doesn't work on his compiler, just apologize and don't argue with him.

There's a lot of very old code out there that is still running, so lots of compilers still accept that style, with coercion. This is the 21st century. You should not write code like that. Leave that code in the previous millennium where it belongs.

I understand that :)

I was just tryin' to see what happens if those excluded.. like if I exclude '#include<stdio.h> part in some computer it works. I think it's okay to do it while learning :D

But thanks for lettin' me know why we should still go with the standard forms. :)

jtbell
Mentor
Did you mean return type cannot be 'void' anymore?
void main() has never been allowed by the C++ standard. However, the C++ standard has existed only since 1998. Before that there was a "de facto" standard based on Bjarne Stroustrup's books. I would be surprised if Stroustrup ever used void main(), but some/many compilers did allow it. Some/many compilers therefore still allow it for backwards compatibilty with heretical code from the "Dark Ages."

jbunniii