warning: missing terminating " character [enabled by default]

Naroju picture Naroju · May 23, 2016 · Viewed 25.6k times · Source

I am getting this weird error below

json.c:81:19: warning: missing terminating " character [enabled by  default]
json.c:81:3: error: missing terminating " character
json.c:82:32: error: expected ‘,’ or ‘;’ before ‘:’ token
json.c:90:22: warning: missing terminating " character [enabled by default]
json.c:90:21: error: missing terminating " character

CODE:

int main()
{
  char * string = "{
                  "sender" : "joys of programming",

                   "receiver": [ "123",
                                 "345",
                                 "654",
                                 "432"
                               ]

                }";
 printf("JSON string: %sn", string);
 json_object * jobj = json_tokener_parse(string);
 json_parse(jobj);
 return 0;
 }

I understood error is about char * string line. But don't know how to fix it.

Answer

LPs picture LPs · May 23, 2016

You have to:

  1. to escape " char because of it is a special char used to define a C-String literal.
  2. For multi line strings you must define each line as a single C-String using "" for each one

So, the resulting code is

  char * string = "{"
                  "\"sender\" : \"joys of programming\","
                  "\"receiver\": [ \"123\","
                                  "\"345\","
                                  "\"654\","
                                  "\"432\""
                                "]"
                 "}";