像 scanf("%d \n %d") 那样扫描两个整数时 \n 有什么用?

What is the use of \n while scanning two integers like in scanf("%d \n %d")?

我在 Hackerrank 的一个编码问题的预完成部分中发现了上述类型的代码。我想知道 \n 会做什么?这有什么区别吗?

读一些不错的C reference website, and perhaps the C11 standard n1570 and probably Modern C

scanf(3) 的文档解释了 \n 在格式控制字符串中发生的情况。它像 space 一样处理并匹配 space 字符序列(例如 ' ',或 '\t',或 '\n')输入流。

如果您明确想要解析行,您可以使用一些解析器生成器,例如 GNU bison and/or use first fgets(3) or getline(3) and later sscanf(3)

不要忘记处理错误情况。请参阅 errno(3). Consider documenting using EBNF 表示程序的有效输入。

研究现有开源程序的源代码以获取灵感,包括GNU bash or GNU make. Be aware than in 2020 UTF-8 should be used everywhere (then you might want to use libunistring whose source code you could study and improve, since it is free software)。

如果您使用 Linux,请考虑使用 gdb(1) or ltrace(1) to understand the behavior of your program. Of course, read the documentation of your C compiler (perhaps GCC) and debugger (perhaps GDB)。