0

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;
}
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
Beeran
  • 33
  • 4
  • 1
    If this was taken from a book, you need a new book. Comparing a `float` variable with double constants such as `90.0` is conceptually wrong and bad. Also, using if-else without braces { } ("compound statement") is dangerous style and shouldn't be taught. And also minor nit-pick: `int main()` is obsolete function declaration style - books shouldn't teach obsolete style. – Lundin Aug 24 '17 at 11:33
  • @Lundin: Your `float` point is an important one, but scientific programmers know that floating point schemes are capable of holding whole numbers exactly. – Bathsheba Aug 24 '17 at 11:35
  • Thanks . Please suggest a good book – Beeran Aug 24 '17 at 11:36
  • started is used to print the newline after each iteration of while loop – suraj Aug 24 '17 at 11:38
  • K&R͏͏͏͏͏͏͏͏͏͏͏͏͏ – Bathsheba Aug 24 '17 at 11:38
  • Oh btw the book is also bad for teaching you to use `int` for boolean types. That's 1980s style C programming. Use `bool` or `_Bool` instead. – Lundin Aug 24 '17 at 11:44
  • @Lundin Can you please suggest a good book for starters? – Beeran Aug 24 '17 at 11:47
  • It's there to distinguish between first and following lines. If you want to separate lines with ",\n" you need to know that there is (at last) one line already. This is why you have started variable. – Oo.oO Aug 24 '17 at 11:47
  • @Bathsheba Even if the floating point standard of the given system were able to represent the result of a `double` calculation in `float` format without introducing oddities, it is still bad, because the code forces the compiler to perform the calculation on a larger type than what's necessary. I very much doubt the compiler can optimize the calculation down to `float`, because in order to do so it must prove internally that the result would be identical. – Lundin Aug 24 '17 at 11:47
  • @Beeran: As Bathsheba already said: [The C Programming Language by the developers of C: K&R](https://www.amazon.com/Programming-Language-2nd-Brian-Kernighan/dp/0131103628). – Andre Kampling Aug 24 '17 at 11:51
  • @Beeran Sorry, I have no idea if any good beginner-level books even exist for C. I can just make anti-recommendations. Avoid K&R like the plague, for example. We do have a book recommendation page https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list but it is full of good books mixed with crap books. – Lundin Aug 24 '17 at 11:52
  • @Andre Thanks a lot. Initially I googled K&R . but got nothing. your suggestion worked. Thanks – Beeran Aug 24 '17 at 11:53
  • @Lundin: What's wrong about K&R, too old? – Andre Kampling Aug 24 '17 at 11:56
  • @Lundin: This odd fish here thinks that K&R ought to be accepted into the biblical canon, but I will concede that you are more of a C expert than I. – Bathsheba Aug 24 '17 at 11:57
  • There's plenty I could say about K&R, few things good, but let's not derail this comment field with book discussions. – Lundin Aug 24 '17 at 12:01

2 Answers2

1

Well this is because there is no such a boolean type in the C language...

so you can do emulate any "boolean" variable, where 0 means false and any other value is considered true

so the block

if (started) { .... }

will only be executed when started is different from 0

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

It's a rather long-winded way of circumventing an initial print of a comma followed by a newline, that's all.

I prefer using a for loop for this kind of thing in order to keep the variable in a tighter scope:

for (
    int started = 0;
    scanf("%f,%f,%79[^\n]", &latitude , &longitude , info ) == 3;
    started = 1
){
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • 1
    That's about as messy as the original code. Less obscure version: `printf(",\n"); int result = 0; while(result!=3) { result = scanf(...); ... }`. do-while would also be fine. – Lundin Aug 24 '17 at 11:40
  • @Lundin: Perhaps I'm just an odd fish but I find my suggestion crystal clear. – Bathsheba Aug 24 '17 at 11:41
  • 2
    You are an odd fish! :) If you look at the original code again, the `started` variable isn't even needed, it is just ugly clutter. – Lundin Aug 24 '17 at 11:43