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: 180

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

w...b

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

Sander

macrumors 6502a
Apr 24, 2008
521
67
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.
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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.
 

killerwhack

macrumors regular
Aug 5, 2004
237
1
Los Angeles, California
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?
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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.
 

Cromulent

macrumors 604
Oct 2, 2006
6,817
1,102
The Land of Hope and Glory
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.