Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# About only one function (C programming)

  1. May 22, 2016 #1
    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?
     
  2. jcsd
  3. May 22, 2016 #2

    QuantumQuest

    User Avatar
    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.
     
  4. May 22, 2016 #3

    Orodruin

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Gold Member

    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.
     
  5. May 22, 2016 #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?
     
  6. May 22, 2016 #5

    Mark44

    Staff: Mentor

    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.
     
  7. May 22, 2016 #6

    jtbell

    User Avatar

    Staff: Mentor

  8. May 22, 2016 #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: May 22, 2016
  9. May 22, 2016 #8

    jim mcnamara

    User Avatar

    Staff: Mentor

    Nothing beats a simple example
    Code (Text):

    // 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.
     
  10. May 23, 2016 #9

    chiro

    User Avatar
    Science Advisor

    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.
     
  11. May 23, 2016 #10
    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: May 23, 2016
  12. May 23, 2016 #11

    Vanadium 50

    User Avatar
    Staff Emeritus
    Science Advisor
    Education Advisor

    I always wondered why this was an int and not an unsigned int.
     
  13. May 23, 2016 #12

    jbunniii

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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.
     
  14. May 23, 2016 #13

    Mark44

    Staff: Mentor

    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.
     
  15. May 23, 2016 #14

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Maintaining backwards compatibility is important!

    Backwards compatibility is also the one of the key reasons every software project eventually turns into a big ball of mud.
     
  16. May 23, 2016 #15

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  17. May 23, 2016 #16

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  18. May 23, 2016 #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: May 23, 2016
  19. May 23, 2016 #18

    Mark44

    Staff: Mentor

    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.
    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.
     
  20. May 24, 2016 #19

    jtbell

    User Avatar

    Staff: Mentor

    Not to mention classic Mac OS, Unix on various systems, etc.
    "int 21? Eh, wuzzat? :oldconfused:"
     
  21. May 24, 2016 #20

    But if we write just main() instead of int main() then still the code runs?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: About only one function (C programming)
Loading...