ZeevKeane
ZeevKeane

Reputation: 75

Which one is a better approach if any?

I'm trying to learn C from the C Programming Language book, in there, one of the exercises is to make a Fahrenheit to Celsius converter.

my code following the books style and instructions is:

#include <stdio.h>
int main() {
   float fhr;
   for (fhr = 0; fhr <= 300; fhr += 20)
      printf("%3.1f %6.1f\n", fhr, (5.0/9.0)*(fhr-32));
}

It says in the book: enter image description here

I just want to know if making a celsius variable and then calling it in the printf function as an argument is better or doing it this way is better, from both human readability and more importantly if it makes any difference to the compiler (I.e. makes the program run any faster or slower.)

Thanks.

Upvotes: 2

Views: 97

Answers (4)

user3629249
user3629249

Reputation: 16550

regarding:

for (fhr = 0; fhr <= 300; fhr += 20)
  printf("%3.1f %6.1f\n", fhr, (5.0/9.0)*(fhr-32));

the 0 and 300 and 20 and 32 are all integers that the code is trying to stuff into a float

the 5.0and9/0aredoubles`

to correct all the above, Suggest:

for ( fhr = 0.0f; fhr <= 300.0f; fhr += 20.0f )
    printf("%3.1f %6.1f\n", fhr, (5.0f / 9.0f )*(fhr-32.0f));

Upvotes: 0

joshmeranda
joshmeranda

Reputation: 3261

From a compiler standpoint it potentially does have an impact. Depending on the compiler, it might see that the variable is only used once and "inline" the value anyway. Many may not, which would cause a hit to the overall performance. That being said, the performance hit would be inconsequential.

As for readability, storing it as its own variable would be easier to look at, and maintain later. Although for a small program like this, the difference is also pretty inconsequential; however, it might start making a difference in larger programs, especially if the value is going to be used more than once.

#include <stdio.h>
int main()
{
   float fhr;
   for (fhr = 0; fhr <= 300; fhr += 20)
   {
      float celsius = (5.0/9.0)*(fhr-32);

      printf("%3.1f %6.1f\n", fhr, celsius);
   }
}

You might also want to consider using a function, to abstract out how this value is determined. Again, this does create a hit to performance, and isn't necessary for such a small program, but would provide access to a way to determine the value from more places in the program. This would mean you would not need to rely on passing the value around, or having the variable within scope:

float fahrenheit_to_celsius(float fhr)
{
    return 5.0 / 9.0 * (fhr - 32)
}

int main()
{
   float fhr;
   for (fhr = 0; fhr <= 300; fhr += 20)
   {
      float celsius = fahrenheit_to_celsius(fhr);

      printf("%3.1f %6.1f\n", fhr, celsius);
   }
}

Upvotes: 3

lfalkau
lfalkau

Reputation: 916

You can also use a function for it, it won't be slower, and it's way better for readability (in my opinion)!

#include <stdio.h>

double fhr_to_cls(double fhr)
{
    return ((5.0 / 9.0) * ( fhr - 32));
}

int main()
{
   double fhr;

   for (fhr = 0; fhr <= 300; fhr += 20)
       printf("%3.1f %6.1f\n", fhr, fhr_to_cls(fhr));
}

Upvotes: 2

sebastian
sebastian

Reputation: 412

Making a variable and then passing it to the printf would surely improve the readability.

From the compiler point of view there's no actual difference. It doesn't affect runtime performances in any way. This is especially true when it comes down to the internal optimizations the compiler carries out.

Upvotes: 4

Related Questions