0

I'm writing a simple web server to handle HTTP 1.1 requests for a class. As such, I've defined some 'stock' strings that I use a lot during the program, eg.

static char* bad_response = // 400, BAD request
    "HTTP/1.1 400 Bad Request\n";

static char* bad_req_body = // 400 error
    "<html><body>\n<h2>Malformed Request</h2>\n"
    "Your browser sent a request I could not understand.\n</body></html>\n";

These strings are defined in the .h file, but only used in the .c file, resulting in the warning "‘bad_response’ defined but not used".

So my question, is there someway to flag these variables so they dont raise that particular warning? Or is proper coding style to just move these variables into the .c file?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ryan
  • 1,451
  • 11
  • 27

2 Answers2

2

Put the strings in the .c file?

Your strings should be placed in the .c file, since they are used in the .c file. In other words, place them where they are intended to be used.

I suggest you take a look at this answer too, which actually states that you could use the keyword extern too.


Flag a variable so that it won't receive a warning?

If you want to completely disable this warning, see this answer. To flag the variable as you say, you could do that:

static char* bad_response __attribute__((unused)) = "400, BAD request";

Which one to use?

I would strongly suggest using the first approach, since the second makes the code less readable (some may be confused by the extra code). If you disable the warning for all variables, then you loose the help of a useful warning, which in other situations might save you.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

The issue here is that the static keyword gives the variable internal linkage, leading to a copy of the variable for each translation unit. You most likely want it to be extern instead and define it in one translation unit. Alternatively, move it into your .c file as the other answer suggests.