Below is the program from Head First C. I failed to understand the purpose of the variable started
and why it was used with if statement:
if (started)
printf(",\n");
else
started = 1
Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float latitude;
float longitude;
char info[80];
int started = 0;
puts("data=[");
while (scanf("%f,%f,%79[^\n]", &latitude , &longitude , info ) == 3) {
if (started)
printf(",\n");
else
started = 1 ;
if ((latitude < -90.0) || (latitude >90.0)) {
fprintf(stderr, "Invalid latitude: %f\n", latitude);
return 2;
}
if ((longitude < -180.0) || (longitude > 180.0)) {
fprintf(stderr, "Invalid longitude: %f\n", longitude);
return 2;
}
printf("{latitude: %f, longitude: %f , info: '%s'}", latitude , longitude , info );
}
puts("\n]");
return 0;
}