Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Issues with setting variables to true or false: how does the compiler understands it Date: Sat, 24 Oct 2015 12:13:16 -0700 Organization: None to speak of Lines: 94 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="18923"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+HTFkhJ+QwYr+5TLm7eEwN" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:9eszBNKXzI+ahvDat/Smk70eXec= sha1:D7fcqKTAYxZ7hYRktcEdBI2TurU= Xref: csiph.com comp.lang.c:73721 Alla _ writes: [...] > My confusion is about the 'started' variable. > > #include > int main() > { > float latitude; > float longitude; > char info[80]; > int started = 0; /**"We need to begin with "started" set > to 0, which means false."**/ > > puts("data=["); > > while (scanf("%f,%f,%79[^\n]", , , ) == 3) > { > if (started) > printf(",\n"); /**"You'll display a comma only if you've > already displayed a previous line.**/ > else > started = 1; /** "Once the loop has started, you can > set "started" to 1, which is true.**/ > > printf("{latitude: %f, longitude: %f, info: '%s'}", , , ); > } > > puts("\n]"); > > return 0; > } The variable started is simply a flag used to keep track of whether we're in the first iteration of the loop or not. Here's a simpler example, a program that counts from 1 to 5 and prints a different message depending on whether it's printing the first line or not: #include int main(void) { int started = 0; for (int i = 1; i <= 5; i ++) { printf("i = %d", i); if (started) { puts(", and we've been here before"); } else { puts(", oh, this is new!"); started = 1; } } } The output is: i = 1, oh, this is new! i = 2, and we've been here before i = 3, and we've been here before i = 4, and we've been here before i = 5, and we've been here before In modern C (post-C99), it's clearer to use a bool rather than an int (changed lines are marked with comments): #include #include /* Add this line */ int main(void) { bool started = false; /* Change int, 0 to bool, false */ for (int i = 1; i <= 5; i ++) { printf("i = %d", i); if (started) { /* No change here */ puts(", and we've been here before"); } else { puts(", oh, this is new!"); started = true; /* change 1 to true */ } } } JSON requires a commma after each element *except* the last. The obvious way to do that would be to print a comma at the end of each line, but omit the comma when you're printing the last line. But it can be difficult or impossible to know whether the current line is the last one -- whereas you can easily detect whether you're on the *first* line by using a flag. So rather than printing a comma at the end of each element except the last, the program prints a comma *before* each element except the first, which achieves the same effect. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"