0
Can anyone explain every code repeated to 'started' variable in the following code?
#include <stdio.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 ; printf("{latitude: %f, longitude: %f, info: '%s'}", latitude, longitude, info); } puts("\n]"); return 0; }
3 ответов
+ 4
Oh I see ...
Well, before the loop begin you assigned 0 for variable <started>. Being zero, this means <started> is evaluable as false e.g. in your `if` branch inside loop body.
When the loop starts, <started> value was 0, so `else` block is executed, which sets <started> to 1. On successive loop iterations, <started> value will be 1, and thus the `if` block will be executed.
The `else` block is only executed once, in the first loop iteration ...
(EDITED)
+ 1
I didn't understand the question. Can you elaborate more clearly please?
0
I mean "if(started) and else started = 1" in this code the first is true and should break but why the else statement still be run?