ccharprintfconversion-specifier

GCC does not recognize escape character '\' on multi-line input


The below will raise compilation warning warning: unknown conversion type character ‘w’ in format [-Wformat=] on a new version of gcc: gcc (Debian 12.2.0-14) 12.2.0

#include <stdio.h>


int main() {
    char query[4096];
    snprintf(query, sizeof(query), "SELECT e.expression_id, e.name, e.channel, e.subsidiary_of, ds.input_channel FROM expression e "
            " JOIN shoe s ON s.shoe_id = e.shoe_id JOIN device_serial ds ON s.device_id = ds.device_id " 
            "WHERE e.name NOT LIKE '\%walker\%' ORDER BY e.channel");
    printf("%s \n", query);
    return 0;
}

It works fine on a shorter input and also on older versions of gcc. How can I make it recognize the backslash as an escape character?


Solution

  • Just write

    "WHERE e.name NOT LIKE '%%walker%%' ORDER BY e.channel"
    

    From the C Standard (7.21.6.1 The fprintf function)

    8 The conversion specifiers and their meanings are:

    % A % character is written. No argument is converted. The complete conversion specification shall be %%.