What is wrong in this code? I get a missing terminating '"' error always. Im new to C and wanted this ASCII for CS50x submissions

huangapple go评论65阅读模式
英文:

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 &#39;&quot;&#39; character [-Werror,-Winvalid-pp-token]
printf(R&quot;EOF(
        ^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
2 errors generated.
#include &lt;stdio.h&gt;
#include &lt;cs50.h&gt;

int main(void)
{
printf(R&quot;EOF(
   _____       _               _ _   _           _   _              _____           _
  / ____|     | |             (_) | | |         | | | |            / ____|         (_)
 | (___  _   _| |__  _ __ ___  _| |_| |_ ___  __| | | |__  _   _  | |    _   _ _ __ ___  __
  \___ \| | | | &#39;_ \| &#39;_ ` _ \| | __| __/ _ \/ _` | | &#39;_ \| | | | | |   | | | | &#39;__| \ \/ /
  ____) | |_| | |_) | | | | | | | |_| ||  __/ (_| | | |_) | |_| | | |___| |_| | |  | |&gt;  &lt;
 |_____/ \__,_|_.__/|_| |_| |_|_|\__|\__\___|\__,_| |_.__/ \__, |  \_____\__, |_|  |_/_/\_\
                                                            __/ |         __/ |
                                                           |___/         |___/
)EOF&quot;);
}

答案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 = &quot;foo\n&quot; &quot;bar\n&quot; &quot;baz&quot;;

And since the whitespace between the string literals is insignificant, we can write the following:

const char *foo = 
    &quot;foo\n&quot;
    &quot;bar\n&quot;
    &quot;baz&quot;;

Vs.

const char *foo = &quot;foo
bar
baz&quot;;

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 = &quot;foo\n\
bar\n\
baz&quot;;

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中,您需要在字符串文字中使用转义序列,如 \\,以输出符号 &#39;\&#39;

否则,请将您的程序编译为C++程序。 What is wrong in this code? I get a missing terminating '"' error always. Im new to C and wanted this ASCII for CS50x submissions

英文:

This construction

R&quot;EOF(
   _____       _               _ _   _           _   _              _____           _
  / ____|     | |             (_) | | |         | | | |            / ____|         (_)
 | (___  _   _| |__  _ __ ___  _| |_| |_ ___  __| | | |__  _   _  | |    _   _ _ __ ___  __
  \___ \| | | | &#39;_ \| &#39;_ ` _ \| | __| __/ _ \/ _` | | &#39;_ \| | | | | |   | | | | &#39;__| \ \/ /
  ____) | |_| | |_) | | | | | | | |_| ||  __/ (_| | | |_) | |_| | | |___| |_| | |  | |&gt;  &lt;
 |_____/ \__,_|_.__/|_| |_| |_|_|\__|\__\___|\__,_| |_.__/ \__, |  \_____\__, |_|  |_/_/\_\
                                                            __/ |         __/ |
                                                           |___/         |___/
)EOF&quot; 

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 &#39;\n&#39; something like the following

  &quot; _____       _               _ _   _           _   _              _____           _\n&quot;
  &quot;/ ____|     | |             (_) | | |         | | | |            / ____|         (_)\n&quot;
 &quot;| (___  _   _| |__  _ __ ___  _| |_| |_ ___  __| | | |__  _   _  | |    _   _ _ __ ___  __\n&quot;
  &quot;\___ \| | | | &#39;_ \| &#39;_ ` _ \| | __| __/ _ \/ _` | | &#39;_ \| | | | | |   | | | | &#39;__| \ \/ /\n&quot;
  &quot;____) | |_| | |_) | | | | | | | |_| ||  __/ (_| | | |_) | |_| | | |___| |_| | |  | |&gt;  &lt;\n&quot;
 &quot;|_____/ \\__,_|_.__/|_| |_| |_|_|\\__|\\__\\___|\\__,_| |_.__/ \\__, |  \\_____\\__, |_|  |_/_/\\_\\\n&quot;

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 &#39;\&#39;.

Otherwise compile your program as a C++ program.:)

huangapple
  • 本文由 发表于 2023年6月26日 04:45:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定