英文:
What is wrong in this code? I get a missing terminating '"' error always. Im new to C and wanted this ASCII for CS50x submissions
问题
我的程序无法正常工作。我遇到了这个错误。
population/ $ make test
test.c:6:9: error: missing terminating '"' character [-Werror,-Winvalid-pp-token]
printf(R"EOF(
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf(R"EOF(
_____ _ _ _ _ _ _ _____ _
/ ____| | | (_) | | | | | | | / ____| (_)
| (___ _ _| |__ _ __ ___ _| |_| |_ ___ __| | | |__ _ _ | | _ _ _ __ ___ __
\___ \| | | | '_ \| '_ ` _ \| | __| __/ _ \/ _` | | '_ \| | | | | | | | | | '__| \ \/ /
____) | |_| | |_) | | | | | | | |_| || __/ (_| | | |_) | |_| | | |___| |_| | | | |> <
|____/ \__,_|_.__/|_| |_| |_|_|\__|\__\___|\__,_|_|_.__/ \__, | \_____\__, |_| |_/_/\_\
__/ | __/ |
|___/ |___/
)EOF");
}
英文:
My program just doesn't work. I get this error.
population/ $ make test
test.c:6:9: error: missing terminating '"' character [-Werror,-Winvalid-pp-token]
printf(R"EOF(
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
printf(R"EOF(
_____ _ _ _ _ _ _ _____ _
/ ____| | | (_) | | | | | | | / ____| (_)
| (___ _ _| |__ _ __ ___ _| |_| |_ ___ __| | | |__ _ _ | | _ _ _ __ ___ __
\___ \| | | | '_ \| '_ ` _ \| | __| __/ _ \/ _` | | '_ \| | | | | | | | | | '__| \ \/ /
____) | |_| | |_) | | | | | | | |_| || __/ (_| | | |_) | |_| | | |___| |_| | | | |> <
|_____/ \__,_|_.__/|_| |_| |_|_|\__|\__\___|\__,_| |_.__/ \__, | \_____\__, |_| |_/_/\_\
__/ | __/ |
|___/ |___/
)EOF");
}
答案1
得分: 6
C中的字符串文字不能跨越多行,除非换行符被转义。
然而,相邻的字符串文字会自动连接,所以下面的方法有效:
const char *foo = "foo\n" "bar\n" "baz";
而且,由于字符串文字之间的空格不重要,我们可以这样写:
const char *foo =
"foo\n"
"bar\n"
"baz";
与以下方法对比:
const char *foo = "foo
bar
baz";
正如在注释中提到的,您可以用反斜杠转义换行符。但在这种情况下,您仍然必须手动插入换行字符。
const char *foo = "foo\n\
bar\n\
baz";
我不喜欢这样做,因为它会阻止代码的缩进,而缩进对于能够跟踪控制流和作用域非常重要。
英文:
String literals in C cannot span multiple lines unless the newlines are escaped.
However, adjacent string literals are automatically concatenated, so the following works:
const char *foo = "foo\n" "bar\n" "baz";
And since the whitespace between the string literals is insignificant, we can write the following:
const char *foo =
"foo\n"
"bar\n"
"baz";
Vs.
const char *foo = "foo
bar
baz";
As mentioned in comments, you can escape the newlines with backslashes. However, in this case you must still remember to insert the newline characters manually.
const char *foo = "foo\n\
bar\n\
baz";
I am not a fan of this as it prevents indentation of code, which is important for being able to follow control flow and scope.
答案2
得分: 0
这个构造表示一个原始字符串文字的概念,在C++中有定义。
在C中不存在这样的构造。
相反,您需要编写带有换行字符 '\n' 的相邻字符串文字,类似于以下方式:
" _____ _ _ _ _ _ _ _____ _\n"
"/ ____| | | (_) | | | | | | | / ____| (_)\n"
"| (___ _ _| |__ _ __ ___ _| |_| |_ ___ __| | | |__ _ _ | | _ _ _ __ ___ __\n"
"\___ \| | | | ' \| '_ \ __/ _` | | '_ \| | | | | | | | | | '__| \ \/ /\n"
"____) | |_| | |_) | | | | | | |_| | | | | | |_| | | |___| |_| | | | |> <\n"
"|_____/ \__,_| .__/|_| |_| \__,_|_|_| |_|\__, | \_____|\__, |_| |_/_/\_\\\n"
等等。
它们将由编译器连接为一个字符串文字。
请注意,在C++中,您可以在原始字符串文字中插入字符,如 \
,它将按原样输出,但在C中,您需要在字符串文字中使用转义序列,如 \\
,以输出符号 '\'
。
否则,请将您的程序编译为C++程序。
英文:
This construction
R"EOF(
_____ _ _ _ _ _ _ _____ _
/ ____| | | (_) | | | | | | | / ____| (_)
| (___ _ _| |__ _ __ ___ _| |_| |_ ___ __| | | |__ _ _ | | _ _ _ __ ___ __
\___ \| | | | '_ \| '_ ` _ \| | __| __/ _ \/ _` | | '_ \| | | | | | | | | | '__| \ \/ /
____) | |_| | |_) | | | | | | | |_| || __/ (_| | | |_) | |_| | | |___| |_| | | | |> <
|_____/ \__,_|_.__/|_| |_| |_|_|\__|\__\___|\__,_| |_.__/ \__, | \_____\__, |_| |_/_/\_\
__/ | __/ |
|___/ |___/
)EOF"
represents a raw string literal the notion of which is defined in C++.
In C such a construction is absent.
Instead you need to write adjacent string literals with new line characters '\n'
something like the following
" _____ _ _ _ _ _ _ _____ _\n"
"/ ____| | | (_) | | | | | | | / ____| (_)\n"
"| (___ _ _| |__ _ __ ___ _| |_| |_ ___ __| | | |__ _ _ | | _ _ _ __ ___ __\n"
"\___ \| | | | '_ \| '_ ` _ \| | __| __/ _ \/ _` | | '_ \| | | | | | | | | | '__| \ \/ /\n"
"____) | |_| | |_) | | | | | | | |_| || __/ (_| | | |_) | |_| | | |___| |_| | | | |> <\n"
"|_____/ \\__,_|_.__/|_| |_| |_|_|\\__|\\__\\___|\\__,_| |_.__/ \\__, | \\_____\\__, |_| |_/_/\\_\\\n"
and so on.
They will be concatenated by the compiler as one string literal.
Pay attention to that in C++ you may insert a character like \
in a raw string literal and it will be outputted as is but in C you need to use escaped sequence in a string literal like \\
to output the symbol '\'
.
Otherwise compile your program as a C++ program.:)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论