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

How to solve this bug?

  1. Oct 6, 2008 #1
    how to solve this bug??

    when i run this code
    instead of getting some expected out put i get

    line 17: `main' must return `int'

    but main is a void function

    it cant return int
    ???
    Code (Text):


    #include <stdio.h>
    int a(int n,int count){
          int i;
          for(i=0;i<n;i++)
               count =a(i,count);
               return count+1;
    }

    int b(int n,int count) {
      int i;
      count =a(n,count);
         for(i=0;i<n;i++)
         count =b(i,count);
         return count;
      }

      void main (){
        int i;
        for (i=0;i<4;i++)
          printf("%d",a(i,0));
          printf("\n%d\n",a(i,10));

          for (i=0;i<4;i++)
            printf("%d",b(i,0));
            printf("\n%d\n",b(i,10));

      }

     
     
  2. jcsd
  3. Oct 6, 2008 #2
    Re: how to solve this bug??

    Why don't just make it return int and add return 0 at the main end?
     
  4. Oct 6, 2008 #3
    Re: how to solve this bug??

    i thought by definition mains had to return something.

    Don't you need to declare those functions too?
     
  5. Oct 6, 2008 #4
    Re: how to solve this bug??

    Like rootX said your best option would be to use int main() instead of void main() and simply have a return 0; at the end of the main function.
     
    Last edited: Oct 6, 2008
  6. Oct 7, 2008 #5

    KTC

    User Avatar

    Re: how to solve this bug??

    No it isn't. In C, and in C++, main() must return int, and 0 is used to indicate successful termination. Part of the confusion arise from the special status granted to main() whereby if the program arrives at the closing }, and there's no explicit return statement, it return 0 implicitly.
     
  7. Oct 7, 2008 #6
    Re: how to solve this bug??

    I think it is good form to always have the main function return a value, but I believe it is implied in ANSI C whereas ANSI C++ requires it explicitly (or is it the other way around?).

    With issues like this it is not uncommon for one compiler to accept it and another to return an error.
     
  8. Oct 7, 2008 #7

    KTC

    User Avatar

    Re: how to solve this bug??

    Both (according to their respective latest standard version) implies it.

     
  9. Oct 7, 2008 #8

    jim mcnamara

    User Avatar
    Science Advisor
    Gold Member

  10. Oct 7, 2008 #9

    KTC

    User Avatar

    Re: how to solve this bug??

    Well, it's not really just C99. C has always required main() to return int. Confusion arise as old C allow one to write
    Code (Text):
    main(void)
    i.e. with no return type. At the time, a function declaration with no explicit return type return an int implicitly.
     
  11. Oct 7, 2008 #10
    Re: how to solve this bug??

    thanks
     
  12. Oct 7, 2008 #11
    Re: how to solve this bug??

    i talked to my teacher and he is 100% percent sure that this code works

    and he doesnt know what i am talking about

    i dont know why he thinks its ok
    because abviosly main has to be int with return 0; in the end
     
  13. Oct 7, 2008 #12
  14. Oct 7, 2008 #13
  15. Oct 7, 2008 #14

    KTC

    User Avatar

    Re: how to solve this bug??

    No no no.

    One, most of the compilers that exist code to C90, which doesn't specify the implementation-defined bit that article you linked to say.
    but some provide void main() as a compiler extension anyway.

    In C99, you already saw
    That mean, yes, the compiler is allowed to let you write "void main()", but they can do that anyway as an compiler extension. If you want to write "void main()", and limit yourself to the compilers that happens to let you do it as an extension to what's guarantee to be safe by the standard, feel free.
     
  16. Oct 7, 2008 #15
    Re: how to solve this bug??

    thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: How to solve this bug?
  1. Bug in iconv? (Replies: 0)

  2. Sneaky bug (Replies: 10)

Loading...