Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

w...b

macrumors regular
Original poster
Jul 14, 2008
187
0
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''
 

Attachments

  • Picture 1.png
    Picture 1.png
    99.2 KB · Views: 184
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
 
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.
 
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.

Thanks for the quick help
 
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.
 
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.

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.
 
Indent the Declarations?

And for the love of God, start indenting better, it will help you later when you get used to it. :)

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?
 
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?

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.
 
Really? I thought it was a pretty horrible book to be honest - didn't really TEACH you anything as such, rather just defined everything.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.