C - 警告:未知转义序列:"\040"[默认启用]



我正在用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 withoutn
 modification, are permitted provided that the following conditions aren
 met:n
n
   * Redistributions of source code must retain the above copyrightn
     notice, this list of conditions and the following disclaimer.n
   * Redistributions in binary form must reproduce the above copyrightn
     notice, this list of conditions and the following disclaimer inn
     the documentation and/or other materials provided with then
     distribution.n
   * Neither the name of XXX and its Subsidiary(-ies) nor the namesn
     of its contributors may be used to endorse or promote products derivedn
     from this software without specific prior written permission.n
n
n
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORSn
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTn
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORn
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTn
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,n
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTn
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,n
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYn
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORTn
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEn
 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: '40' [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 withoutn
 modification, are permitted provided that the following conditions aren
 met:n
n
   * Redistributions of source code must retain the above copyrightn
     notice, this list of conditions and the following disclaimer.n
   * Redistributions in binary form must reproduce the above copyrightn
     notice, this list of conditions and the following disclaimer inn
     the documentation and/or other materials provided with then
     distribution.n
   * Neither the name of XXX and its Subsidiary(-ies) nor the namesn
     of its contributors may be used to endorse or promote products derivedn
     from this software without specific prior written permission.n
nn
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORSn
 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOTn
 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FORn
 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHTn
 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,n
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTn
 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,n
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYn
 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORTn
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USEn
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."n
nnn";
    fputs("n", stderr);
    fputs(license, stderr);
    fputs("n", stderr);
}

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

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

"\

";

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

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

例如

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

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

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

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

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

要理解这一点,请考虑此示例。

程序:

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

编译:说"未知转义序列"

输出: +

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

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

printf("n \+");

现在输出将是

输出: +

谢谢 <3

最新更新