Re: Steve Summit C notes , exercise
- From: Thad Smith <ThadSmith@xxxxxxx>
- Date: Sat, 17 Mar 2007 12:50:04 -0700
Jason Curl wrote:
arnuld wrote:
#include <stdio.h>
#define DIVISOR 10
int main()
{
int i;
double sum;
You can probably make this an int. The square of an int is still an int (providing no overflows). It's generally faster using ints than doubles.
It certainly is sufficient for this particular exercise, but speed shouldn't be an issue here. I consider it a matter of style. I would use double here for this type of simple program so that I don't have to worry about overflow with a larger limit or truncation on division.
printf("the average is: %.1f\n", sum / DIVISOR);
if you take my advice above about using an int above, just be careful to promote sum to a float here, e.g. (float)sum / DIVISOR
That works, but I use double as the preferred floating point type. If I choose float (or long double), it is for a specific reason.
--
Thad
.
- References:
- Steve Summit C notes , exercise
- From: arnuld
- Re: Steve Summit C notes , exercise
- From: Jason Curl
- Steve Summit C notes , exercise
- Prev by Date: Re: where does the value returned by main go ?
- Next by Date: Re: strtok
- Previous by thread: Re: Steve Summit C notes , exercise
- Next by thread: Re: Steve Summit C notes , exercise
- Index(es):
Relevant Pages
|