0
#include "stdio.h"
int main(){
    float longi;
    float lati;
    char info[80];
    int started = 0;

    puts("Data=[");
    while((scanf("%f\n%f\n%s",&lati,&longi,info))==3){ //here is my doubt.


        printf("\n{latitude:%f, longitude:%f, info:%s},",lati, longi, info);
    }
    puts("\n]");    
}

The above code works as desired, but I figured it out by trial and error. This takes three inputs with the 'enter' key as separator. Initially I supplied:

'\n' instead of \n

in the format specifier, but that was of no use; it was taking only one input every time.

What's the difference between these two and how does scanf() handle them?

pat
  • 12,587
  • 1
  • 23
  • 52
OldSchool
  • 2,123
  • 4
  • 23
  • 45

1 Answers1

2

So you had a scanf("%f'\n'%f'\n'%s",&lati,&longi,info) in your code.

When you break this code up:

  • %f - expect a float
  • ' - expect a literal ' in the input
  • \n - expect a line break
  • ' - expect a literal ' in the input

... and so on.

\n is a linebreak. Unquoted. The reason that some manuals show it in single quotes is a difference between a char that's represented by two symbols in code and a string literal.

See this question for more explanation.

Proper code is the one you already stated yourself:

scanf("%f\n%f\n%s",&lati,&longi,info)

Community
  • 1
  • 1
favoretti
  • 29,299
  • 4
  • 48
  • 61
  • `\n` matches any sequence of white space (and is redundant here). To match a literal newline, `%*1[\n]` is needed – mafso Feb 12 '15 at 12:55