Search code examples
gccsuppress-warnings

How to disable warning: null character(s) preserved in literal?


Basically I want to ignore warnings about null-chars in literals.

GCC says that this warning is enabled by default.

I can't find an appropriate -Wno- option for this warning.

You get such warnings in code like this

const char input[] = "abc^@123";

where ^@ denotes the ASCII 0 character (you can enter it with vim via Ctrlv0 - and verify it via ga in command mode while the cursor is placed over the character).


Solution

  • It's happening in the lexer, and it doesn't look like you can disable the check. :-( Here's the code snippet from libcpp/lex.c:

    if (saw_NUL && !pfile->state.skipping)
      cpp_error (pfile, CPP_DL_WARNING,
             "null character(s) preserved in literal");
    

    As you can see, there's no check of any options before emitting the warning.