1

While working with JSON parsing I noticed that the char type data string is written like this:

char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; 

Why there are (escapes) " ---" used in the code? Why can't we use the string normally?

1
  • Try it without and see what happens ;-)
    – SBF
    Dec 29 '20 at 12:22
4

Why there are (escapes) " ---" used in the code? Why can't we use the string normally?

Because escapes are required.

In C " means 'The start or end of a string', but \" means 'A double-quote character'.

There has to be a way to distinguish a double-quote character from a string delimiter so the compiler knows which is which. Without escapes your string would look like:

char json[] = "{"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}"; 

and the compiler would find the end of the string at the second ", and then a load of junk before it finds the end-of-statement ; character. It can't work out what sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}" is supposed to be, since it isn't in the string, but after it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.