# Re: Degrees Exercise.. Wha am I missing?

#1

In the exercise we create the following code:

[code]#include <stdio.h>
#include <stdlib.h>

//Declare a static variable
static float lastTemperature;
//Initialize lastTemperature to 50 degrees
static float lastTemperature = 50.0;

float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);
return fahr;
}

int main (int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %f degrees Fahrenheit\n”, freezeInF);
printf(“The last temperature converted was %f\n”, lastTemperature);
return EXIT_SUCCESS;
}
[/code]

However, due to this line in the main function:

float freezeInF = fahrenheitFromCelsius(freezeInC);

The result will never be anything other than:

0.000000 Celsius is 32.000000 Fahrenheit
Water freezes at 32.000000 degrees Fahrenheit
The last temperature converted was 0.000000

So defining the static variable lastTemperature seems to be irrelevant.

What am I missing here?

-Thomas

#2

It is sort of irrelevant. At this point, you are still getting the basics. Keep going.

#3

Thanks Aaron

#4

To dakar303.

Its all about the order in which you place things within your code. Remember that compilers read from top-to-bottom and execute things accordingly.

So to make things clear. I am going to add a new line of code. Now run both of them in your compiler to see what I actually mean.

In the first code:
The the new line of code I added BEFORE the compiler executed the “float freezeInF = fahrenheitFromCelsius(freezeInC)” line, and the return value will be 50.

[code]
#include <stdio.h>
#include <stdlib.h>

//Declare a static variable
static float lastTemperature;
//Initialize lastTemperature to 50 degrees
static float lastTemperature = 50.0;

float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);
return fahr;
}

int main (int argc, const char * argv[])
{

//This is the new line of code I added BEFORE the compiler executed the “float freezeInF = fahrenheitFromCelsius(freezeInC)” line.
printf(“The declared value of lastTemperature is %f\n”, lastTemperature);

``````float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);
printf("The last temperature converted was %f\n", lastTemperature);

return EXIT_SUCCESS;
``````

}[/code]

In the Second code:
The the new line of code I added AFTER the compiler executed the “float freezeInF = fahrenheitFromCelsius(freezeInC)” line, and the return value will be 0 (zero).

[code]
#include <stdio.h>
#include <stdlib.h>

//Declare a static variable
static float lastTemperature;
//Initialize lastTemperature to 50 degrees
static float lastTemperature = 50.0;

float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);
return fahr;
}

int main (int argc, const char * argv[])
{

``````float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf("Water freezes at %f degrees Fahrenheit\n", freezeInF);
printf("The last temperature converted was %f\n", lastTemperature);
``````

//This is the new line of code I added AFTER the compiler executed the “float freezeInF = fahrenheitFromCelsius(freezeInC)” line.
printf(“The declared value of lastTemperature is %f\n”, lastTemperature);

``````return EXIT_SUCCESS;
``````

}[/code]

Hopefully this clears things up.

#5

To clear out any confusion that might arise from above:

Strictly speaking, compilers don’t execute the source code they compile.

Compilers generate code, reading as input a program’s source files (and in the link phase, one or more libraries.) It is this generated code that gets executed on the target machine.

#6

Hey I have a question about what `return fahr;` actually does

For the same excerise I had this down (shown in the book)

[code]#include <stdio.h>

// Decalre a global variable
float lastTemperature;

float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit.\n", cel, fahr);
}

int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %f degrees Fahrenheit.\n”, freezeInF);
printf(“The last temperature converted was %f.\n”, lastTemperature);
return 0;
}
[/code]

and I kept getting this

[quote]0.000000 Celsius is 32.000000 Fahrenheit.
Water freezes at 0.000000 degrees Fahrenheit.
The last temperature converted was 0.000000.[/quote]

[code]#include <stdio.h>

// Decalre a global variable
float lastTemperature;

float fahrenheitFromCelsius(float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit.\n", cel, fahr);
return fahr;
}

int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %f degrees Fahrenheit.\n”, freezeInF);
printf(“The last temperature converted was %f.\n”, lastTemperature);
return 0;
}[/code]

and finally got a correct output

[quote]0.000000 Celsius is 32.000000 Fahrenheit.
Water freezes at 32.000000 degrees Fahrenheit.
The last temperature converted was 0.000000.[/quote]

What did the code do to correct it ?