Undeclared Identifier Global Variable


#1

Hello everybody,
on the section about adding a global variable I get the error “use of undeclared identifier ‘lastTemperature’” in the lines “lastTemperature = cel;” and “printf(“The last temperature converted was %f\n”, lastTemperature);” . Can anybody please help? I’m using Xcode 4.5.2.
What am I doing wrong?

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

// Declare a global variable float lastTemperature;

float fahrenheitFromCelsius (float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 +32;
printf("%f Celsiusis %f in Fahrenheit", 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", freezeInF);
printf("The last temperature converted was %f\n", lastTemperature);

return EXIT_SUCCESS;

}
/code]


#2

You simply forgot to declare the global variable. You entered a comment about your intention but did not declare it.

You can declare it like this:

...
// Declare our global variable to keep track of the last temperature
static float lastTemperature;
...

You should also post your code between proper code tags to make it easier for others to read your code.


#3

Thank you so much Ibex10. I have the kindle edition of the Objective-C book and somehow misread the declaration of the variable as part of the comment; having no prior experience with programming I thought the declaration of the global variable was actually the line within the “float fahrenheitFromCelsius” function frame “lastTemperature = cel;” which I now realize is of course a wrong assumption. Thanks for your patience. I’ll figure out how to properly post code too (first post).