警告:未知的转义序列:'\040' [默认情况下启用]

6

我正在使用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;
}

你有没有谷歌搜索过“转义序列040”? - Weather Vane
@WeatherVane:是的,我试过了 - 但是添加另一个 \\ 字符只会导致不将许可证文本作为整个文本来打印。 - yak
1
你的一行或多行代码末尾有 "\[space]" 吗?可以尝试这个答案:http://bytes.com/topic/c/answers/712378-why-backslash-space-combo-escape-sequence-040-a 如果是这样,请将 "\ " 替换为 "",这会告诉编译器在下一行继续处理文本。 - Weather Vane
@WeatherVane:我末尾没有空格,只有"\\[Enter]",所以实际上并没有帮助(已尝试)。 - yak
3个回答

12

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

"\n\ \n";

反斜杠空格不是有效的转义序列。这个 "040" 消息是一个八进制空格字符,由前导 0 表示。


有些棘手。我遇到了类似的情况。如果现在使用最新的gcc 4.8.5编译mscgen-0.20应用程序,你会得到"""usage.c:79:1: warning: unknown escape sequence: '-' [enabled by default]"""。将第61行的-更改为-即可解决问题。 - gaoithe

2

看起来在许可证的定义中,反斜杠和换行符之间有一个空格。

例如:

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

或者在多行定义的其他行中也可能会出现。

以下是更简单的定义方式:

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

-1

这个警告通常是由于反斜杠[]后面跟着一个空格,因此当我们将其视为警告时,它会忽略“所有后面跟着空格的反斜杠”。从而导致缺少模式...

要解决这个问题,我们只需要在属于这个类别的反斜杠旁边添加另一个反斜杠,这样可以帮助打印出在空格之前的下一个反斜杠。

为了理解这一点,请考虑以下示例...

程序:

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

编译错误:显示“未知的转义序列”

输出结果:+

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

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

i.e

printf("\n \\+");

现在的输出结果为:

输出:+

谢谢 <3


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接