warning: unknown escape sequence: '\040' [enabled by default]
Asked Answered
A

3

6

I'm writing a simple application in C, which I would like to publish under BSD license. One part of the application is responsible for printing out information about the program to its users. However, I have a problem with printing out the license text. Here's the example:

#include <stdio.h>
#include <stdlib.h>

void show_license(void)
{
    const char *license = "\n\
 Copyright (c) 2012 \n\
 All rights reserved.\n\
 \"Redistribution and use in source and binary forms, with or without\n\
 modification, are permitted provided that the following conditions are\n\
 met:\n\
\n\
   * Redistributions of source code must retain the above copyright\n\
     notice, this list of conditions and the following disclaimer.\n\
   * Redistributions in binary form must reproduce the above copyright\n\
     notice, this list of conditions and the following disclaimer in\n\
     the documentation and/or other materials provided with the\n\
     distribution.\n\
   * Neither the name of XXX and its Subsidiary(-ies) nor the names\n\
     of its contributors may be used to endorse or promote products derived\n\
     from this software without specific prior written permission.\n\
\n\
\n\
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\
 \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n\
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n\
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n\
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n\
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n\
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n\
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n\
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n\
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n\
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\"\n\
\n\
\n\ \n";

    fputs("\n", stderr);
    fputs(license, stderr);
    fputs("\n", stderr);
}


int main()
{
    show_license();
    return 0;
}

I compile my application using gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.11 on Kubuntu 13.10. I got this warning message:

warning: unknown escape sequence: '\040' [enabled by default]
     const char *license = "\n\
                           ^

How can I get rid of it? I promised to myself to write a code without any warnings and errors. It's a plain C application.

EDIT:

Thank you all, here's the properly working, warnings-free code:

#include <stdio.h>
#include <stdlib.h>

void show_license(void)
{
    const char *license = "\n \
 Copyright (c) 2012 \n\
 All rights reserved.\n\
 \"Redistribution and use in source and binary forms, with or without\n\
 modification, are permitted provided that the following conditions are\n\
 met:\n\
\n\
   * Redistributions of source code must retain the above copyright\n\
     notice, this list of conditions and the following disclaimer.\n\
   * Redistributions in binary form must reproduce the above copyright\n\
     notice, this list of conditions and the following disclaimer in\n\
     the documentation and/or other materials provided with the\n\
     distribution.\n\
   * Neither the name of XXX and its Subsidiary(-ies) nor the names\n\
     of its contributors may be used to endorse or promote products derived\n\
     from this software without specific prior written permission.\n\
\n\n\
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n\
 \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n\
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n\
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n\
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n\
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n\
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n\
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n\
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n\
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n\
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\"\n\
\n\n\n";

    fputs("\n", stderr);
    fputs(license, stderr);
    fputs("\n", stderr);
}


int main()
{
    show_license();
    return 0;
}
Animosity answered 28/12, 2014 at 19:25 Comment(4)
Have you googled "escape sequence 040"?Shulman
@WeatherVane: Yes, I did - but adding another `\` character simply results in not-treating the license text as a whole text to print outAnimosity
Do you have "\[space]" at the end of one or more lines? Try this answer bytes.com/topic/c/answers/… If so, replace "\ " with "\" which tells the compiler to continue the literal text on the next line.Shulman
@WeatherVane: I don't have space at the end, just "\\[Enter]", so it didn't help actually (tried)Animosity
S
12

In your code you have this line (the last line of the agreement text) which is what is causing the error:

"\n\ \n";

Backslash-space is not a valid escape sequence. The message "040" is a space char in octal, signified by the leading 0.

Shulman answered 28/12, 2014 at 19:52 Comment(1)
Tricky to find. I encountered something similar. If compiling mscgen-0.20 application now with latest gcc 4.8.5 you get """usage.c:79:1: warning: unknown escape sequence: '\-' [enabled by default]""" Changing the \- to - on line 61 solves the problem.Belvia
E
2

It seems that somewhere in the definition of license there is a blank after \ and before the new line character.

For example

const char *license = "\n\
                          ^^^ here is a blank  

or maybe in some other line of the multiline definition.

It would be simpler to write the definition the following way

    const char *license = "\n"
                          "Copyright (c) 2012 \n"
//...
Exorcise answered 28/12, 2014 at 19:50 Comment(0)
W
-1

This warning basically arises when you have a backslash [] followed by a space so, when we execute it considering it only to be a warning it misses "ALL THE BACKSLASHES THAT ARE FOLLOWED BY SPACES". thus making it a missing pattern ... To fix this we just need to add another BACKSLASH beside the backslashes that come under this category thus this helps in printing the next back slash after the one that was before the space.

To understand this consider this example..

program:

#include<stdio.h>
void main()
{
printf("\n \+");
}

compiliation: says "unknown escape sequence"

output: +

------------------------------------------------------------------------------

cosider the same code but in printf statement use another backslash in between [\ & +]

i.e

printf("\n \\+");

now the output would be

output: +

Thankyou <3

Woebegone answered 25/6, 2019 at 10:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.