Im new to C. And i keep getting this warning in xCode. Can you help?

Discussion in 'Mac Programming' started by w...b, Nov 13, 2008.

  1. w...b macrumors regular

    Joined:
    Jul 14, 2008
    #1
    So im reading one of these eBooks for C programming. I have done "hello world' (yes i am a complete beginner) and now im doing this one that prints at a calculation of temperatures - F vs C.

    But i keep getting this warning - 'return types defaults to 'int''
     

    Attached Files:

  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    for main's signature, use:
    Code:
    int main(int argc, char *argv[])
    rather than
    Code:
    main()
    I don't think we need a debate about what's "most right", but it will be easier for you and anyone that reads your code if you use the signature posted above for main.

    -Lee
     
  3. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #3
    your main() function should be declared (minimally) as

    int main()


    Pre-ANSI C allowed main to be declared without a return type, or a return type of void. These days, you declare it as returning an int.
     
  4. w...b thread starter macrumors regular

    Joined:
    Jul 14, 2008
    #4
    Thanks for the quick help
     
  5. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #5
    By the way: if your ebook is advocating the old-fashioned main(), it may have other flaws. It's not like the proper signature has become standard last week or so.

    Disclaimer: I wrote a programming book myself so I am probably biased.
     
  6. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    He is using "The C Programming Language" So it is a good quality book :).

    I recognise the exercise.

    Edit: Technically it should be

    int main(void) or int main(int argc, char *argv[]) they are the only two correct usages according to the standard. Everything else is implementation defined.
     
  7. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    Is the 3rd optional parm for environment variables not standard?
     
  8. iShater macrumors 604

    iShater

    Joined:
    Aug 13, 2002
    Location:
    Chicagoland
    #8
    And for the love of God, start indenting better, it will help you later when you get used to it. :)
     
  9. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #9
    No that is an OS level thing not a language feature.
     
  10. killerwhack macrumors regular

    killerwhack

    Joined:
    Aug 5, 2004
    Location:
    Los Angeles, California
    #10
    Indent the Declarations?

    What is your opinion on the subject question? Is it better to indent everything between the braces ?

    What do you think about having the first brace at the end of the line above rather than on it's own line?
     
  11. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #11
    You always indent another level for another layer of braces. This is a simple example:

    Code:
    int main(void)
    {
        //blah
        if(something == 5)
        {
             //blah
             if(somethingelse == 7)
             {
                 //bah
             }
        }
    }
    annoyingly tabs don't work properly on this forum. But that should be one tab indent per layer. Most programmers (well the open source projects I have looked into) equate one tab with four spaces.

    Some people prefer to declare functions like this though:

    Code:
    int
    main(void)
    so you can see the name clearly on the left hand side.

    It is just worth reading some coding standards guidelines from some open source projects. I recommend reading through the OpenBSD source code as it is very clear and easy to understand code compared to some other open source projects.
     
  12. fluidedge macrumors 65816

    fluidedge

    Joined:
    Nov 1, 2007
    #12
    Really? I thought it was a pretty horrible book to be honest - didn't really TEACH you anything as such, rather just defined everything.
     
  13. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #13
    It's the best programming book I've ever read.

    If you read it, it teaches you everything you need to know about the language in less than 300 pages. Pretty big achievement when most programming books are a painful 1000+ pages long.
     

Share This Page