gpt4 book ai didi

c++ - 来自 C 的指令 #include 和来自 C++ 的指令之间有什么区别吗?

转载 作者:太空狗 更新时间:2023-10-29 19:37:22 26 4
gpt4 key购买 nike

No 这个问题没有在帖子中回答 #include <filename> 之间有什么区别?和 #include “filename” ? 这是一个不同的问题。我正在研究 C 和 C++ 之间的差异。我通过比较您可以用每种语言制作的最基本的程序来做到这一点:

在 C 中:

#include <stdio.h>
int main()
{
printf("Hello World");
return 0;
}

在 C++ 中

#include <iostream>
int main()
{
std::cout << "Hello World!!!" << std::endl;
return 0;
}

我知道 header 和编译过程。但我想知道 C 和 C++ 的 #include 指令之间是否有任何区别。例如,复制时的标题内容可能以不同的方式复制。我认为这个问题非常简单,您可以回答“否”或“是,区别如下:1), 2)”。

最佳答案

是的,至少有两个区别。在 C++ 中(WG21 N4567 [cpp.include]/5):

The implementation shall provide unique mappings for sequences consisting of one or more nondigits or digits (2.10) followed by a period (.) and a single nondigit. The first character shall not be a digit. The implementation may ignore distinctions of alphabetical case.

在 C 中(WG14 N1570 6.10.2/5,强调我的):

The implementation shall provide unique mappings for sequences consisting of one or more nondigits or digits (6.4.2.1) followed by a period (.) and a single nondigit. The first character shall not be a digit. The implementation may ignore distinctions of alphabetical case and restrict the mapping to eight significant characters before the period.

符合标准的 C 实现可以将“foobarbaz.h”和“foobarbat.h”映射到同一个源文件。符合标准的 C++ 实现不能。


此外,在 C (N1570 6.4.7) 中:

If the characters ', \, ", //, or /* occur in the sequence between the < and > delimiters, the behavior is undefined. Similarly, if the characters ', \, //, or /* occur in the sequence between the " delimiters, the behavior is undefined.

在 C++ 中(N4567 [lex.header]/2):

The appearance of either of the characters ' or \ or of either of the character sequences /* or // in a q-char-sequence or an h-char-sequence is conditionally-supported with implementation-defined semantics, as is the appearance of the character " in an h-char-sequence.

“有条件地支持实现定义的语义”意味着

  • 如果实现不支持它,它必须发出诊断;
  • 如果实现确实支持它,则必须记录它对该构造的解释。

而“未定义的行为”意味着实现可以做任何它想做的事。

关于c++ - 来自 C 的指令 #include 和来自 C++ 的指令之间有什么区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36290995/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com