how to solve this bug??

This is a discussion on how to solve this bug?? within the C Programming forums, part of the General Programming Boards category; when i run this code instead of getting some expected out put i get line 17: `main' must return `int' ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    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:
    #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. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually it is telling you that try as you may, you aren't supposed to ever have it be void.

    Just change it to int. The return value is optional.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    but its a void function

    i dont want it to return integer

    why its a bug
    why main cannot be void??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The people (ANSI/ISO committee) who are in charge of C say so. And for that matter, you may not want it to return an integer, but Windows/Linux/OS X/various other operating systems certainly do.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It isn't a bug per se, so much as its just breaking of the standard. In other words, how can it be a bug if it won't compile without complaint? I would reserve the word bug for those pesky things that your compiler doesn't mention to you.

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i copied this code from a test question which asks to find the output of this code

    and the answer is some numbers

    there is no way that this code has a bug
    i cant see what did i missed

    this is the original stuff
    did i missed some thing

    http://img216.imageshack.us/my.php?i...5165572gm4.gif

    ??

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Think that when the main program finishes it will return an int. That information might be helpful or even vital for the OS to know if the program succeeded or not. The default is return 0

  8. #8
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by transgalactic2 View Post
    i copied this code from a test question which asks to find the output of this code

    and the answer is some numbers

    there is no way that this code has a bug
    i cant see what did i missed

    this is the original stuff
    did i missed some thing

    http://img216.imageshack.us/my.php?i...5165572gm4.gif

    ??
    Generally, in C both types can be used. Either void main or int main. Just int main is preferred so why not use it? You are getting a warning or an error?

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    The first thing you need to realise is the world is knee deep in idiots who think void main (and lots of other horrors) are perfectly valid C.

    Unfortunately, many of these people tend to write books or teach at 2nd rate universities, rather than be professional programmers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok lets break this into a more general thing.

    You: Matt how do you enter a C program?
    Me: Well Billy, you simply create a main() function.
    You: main() function?
    Me: According to the C standard, a main() function can take one of two forms.
    You: Can you post them in code tags since you can't possibly demonstrate this concept verbally?
    Me: Haha Billy, of course I can.

    (Matt pats Billy on the head)

    Example:
    Code:
    /*
     *  First form: With no parameters.
     */
    int main(void)
    {
      return 0;
    }
    
    /*
     * Second form: With parameters.
     */
    int main(int argc, char **argv)
    {
      return 0;
    }
    Anything else would be wrong.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Untitled2.c|17|error: `main' must return `int'|
    ||=== Build finished: 1 errors, 0 warnings ===|

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So change it to int, already.

    (It won't affect your instructor's answer.)

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Again, be proud that your compiler absolutely refuses to accept your function. Many will demote this practice down to a mere warning.

    Again I say just change the void to an int and your code will compile with no other changes to your code.

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Is there some problem with the dictionary definition of "must" which is troubling you?

    Just change the void to int, and put a return 0; at the end and get on with life!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    this is what he wrote

    this is the code i must inspect

    i get the idea that he made a mistake
    thanks

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  3. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  4. Annoying bug I can't find or fix!
    By homeyg in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2004, 11:13 PM
  5. Recource Script Bug in VC++
    By Xzyx987X in forum Windows Programming
    Replies: 1
    Last Post: 04-24-2004, 09:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21