警告:未知逃脱序列:' \ 040' [默认启用]

时间:2014-12-28 19:25:46

标签: c string warnings

我正在用C编写一个简单的应用程序,我想在BSD许可下发布。应用程序的一部分负责向用户打印有关该程序的信息。但是,我在打印许可证文本时遇到问题。这是一个例子:

#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;
}

我在Kubuntu 13.10上使用gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.11编译我的应用程序。我收到了这条警告信息:

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

我该怎样摆脱它?我答应自己写一个没有任何警告和错误的代码。这是一个简单的C应用程序。

修改

谢谢大家,这是正常工作,无警告的代码:

#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;
}

3 个答案:

答案 0 :(得分:5)

在你的代码中,你有这一行(协议文本的最后一行)是导致错误的原因:

&#34; \ n \ \ n&#34;;

反斜杠空格不是有效的转义序列。消息&#34; 040&#34;是八进制中的空格char,由前导0表示。

答案 1 :(得分:1)

似乎在许可证定义的某个地方,在\和新行字符之前有一个空白。

例如

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

或者可能在多线定义的其他一行中。

以下列方式编写定义会更简单

    const char *license = "\n"
                          "Copyright (c) 2012 \n"
//...

答案 2 :(得分:-1)

当您在反斜杠[]后面加一个空格时,基本上会出现此警告,因此,当我们执行它时,仅将其视为警告,就会错过“所有空格引起的反斜杠”。从而使其成为一种缺失的模式... 要解决此问题,我们只需要在该类别下的反斜杠旁边添加另一个BACKSLASH即可,这有助于在空格之前的反斜杠之后打印下一个反斜杠。

要了解这一点,请考虑以下示例。

程序:

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

编译:说“未知的转义序列”

输出:+

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

使用相同的代码,但在printf语句之间使用另一个反斜杠 [\&+]

printf("\n \\+");

现在的输出将是

输出:+

谢谢<3