About only one function (C programming)

In summary: The integer expression is mostly for the operating system which uses it to determine how the program executed.The int main() function is the function the compiler looks for when it wants to know where your program should start executing the code.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?return 0; is the usual return type for a main() function in C. The return value is 0, which means the program ended successfully.If you don't have a return x; statement
  • #1
fireflies
210
12
I have learned 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?
 
Technology news on Phys.org
  • #2
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.
 
  • #3
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.
 
  • #4
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?
 
  • #5
fireflies said:
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.
 
  • #6
  • #7
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:
  • #8
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.
 
  • #9
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.
 
  • Like
Likes fireflies
  • #10
fireflies said:
I have learned 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:
  • #11
chiro said:
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.
 
  • #12
Vanadium 50 said:
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. :biggrin:) 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.
 
  • #13
jbunniii said:
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.
 
  • #14
jbunniii said:
Changing the return type of main would have broken every program in existence at that point. (All five of them. :biggrin:)
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.
 
  • #15
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.
 
  • Like
Likes Pepper Mint
  • #16
Vanadium 50 said:
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.
 
  • #17
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:
  • #18
rootone said:
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.
 
  • Like
Likes Pepper Mint
  • #19
rootone said:
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? :oldconfused:"
 
  • #20
chiro said:
The integer expression is mostly for the operating system which uses it to determine how the program executed.

jbunniii said:
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. :biggrin:)
But if we write just main() instead of int main() then still the code runs?
 
  • #21
fireflies said:
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.
 
  • Like
Likes fireflies
  • #22
fireflies said:
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.
 
  • #23
fireflies said:
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.
 
  • Like
Likes fireflies
  • #24
fireflies said:
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
 
  • Like
Likes fireflies
  • #25
jbunniii said:
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?
 
  • #26
jtbell said:
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 said:
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. :)
 
  • #27
fireflies said:
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." :devil:
 
  • #28
jtbell said:
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." :devil:
If I recall correctly, void main() has never been allowed by the C standard, either. Many (perhaps most) compilers tolerate it, however.
 
  • #29
I guess that where it's tolerated the compiler is simply substituting the void with a returned value of zero.
 

FAQ: About only one function (C programming)

1. What is a function in C programming?

A function in C programming is a block of code that performs a specific task. It is a reusable piece of code that can be called from different parts of a program, making it easier to organize and manage code.

2. How do you declare a function in C programming?

To declare a function in C programming, you need to specify the function's return type, name, and any parameters it takes. This is done in the form of a function prototype, which is typically placed at the top of the program before the main() function.

3. What is the difference between a function declaration and a function definition?

A function declaration is a statement that tells the compiler about the function's name, return type, and parameters. It is used to inform the compiler about the existence of a function before it is used in the program. On the other hand, a function definition contains the actual code for the function and is used to define how the function behaves.

4. How do you call a function in C programming?

To call a function in C programming, you need to use the function name followed by parentheses and any required arguments. The function call can be made from within another function or the main() function.

5. Can a function return more than one value in C programming?

No, a function in C programming can only return one value. However, you can use pointers to pass multiple values by reference to a function and modify them within the function.

Similar threads

Back
Top