title: Extracting key=value with scanf in C
body: <p>I need to extract a value for a given key from a string. I made this quick attempt:</p>
  
  <pre><code>char js[] = "some preceding text with\n"
      "new lines and spaces\n"
      "param_1=123\n"
      "param_2=321\n"
      "param_3=string\n"
      "param_2=321\n";
  
  char* param_name = "param_2";
  char *key_s, *val_s;
  char buf[32];
  
  key_s = strstr(js, param_name);
  
  if (key_s == NULL)
      return 0;
  
  val_s = strchr(key_s, '=');
  
  if (val_s == NULL)
      return 0;
  
  sscanf(val_s + 1, "%31s", buf);
  
  printf("'%s'\n", buf);
  </code></pre>
  
  <p>And it in fact works ok (<code>printf</code> gives <code>'321'</code>). But I suppose the <code>scanf</code>/<code>sscanf</code> would make this task even easier but I have not managed to figure out the formatting string for that. </p>
  
  <p>Is that possible to pass a content of a variable <code>param_name</code> into <code>sscanf</code> so that it evaluates it as a part of a formatting string? In other words, I need to instruct <code>sscanf</code> that in this case it should look for a pattern <code>param_2=%s</code> (the <code>param_name</code> in fact comes from a function argument).</p>