在这里,我们将看到一些程序,如果它们在 C 或 C++ 编译器中编译,将返回不同的结果。我们可以找到许多这样的程序,但在这里我们讨论其中一些。在 C 和 C++ 中,字符文字的处理方式不同。在 C 中,它们被视为 int,但在 C++ 中,它们被视为字符。因此,如果我们使用 sizeof() 运算符检查大小,它将在 C 中返回 4,在 C++ 中返回 1。示例 在线演示#include <stdio.h> int main() { printf("The character: %c, size(%d)", 'a', sizeof('a')); }输出The character: a, size(4)示例#include <stdio.h> int main() { printf("The ... 阅读更多
在 C++ 中,我们使用 getline() 函数从流中读取行。它接收输入,直到按下 Enter 键或给出用户指定的定界符。在这里,我们将了解如何使用 getline() 函数将换行符作为输入。让我们看看下面的实现来了解一下。示例 在线演示#include <iostream> using namespace std; int main() { string str; int term = 4; while (term--) { getline(cin, str); while (str.length()==0 ) getline(cin, str); cout<<……