[英]Do c and c++ use the same library?
我正在编写 c++ 代码并开始使用“strcmp”命令,但我不知道是否应该像 c 那样包含 string.h 库。
c++ 代码使用 'strcmp' 不包括 string.h 库,但没有错误。 为什么没有出现错误?
Student* StdSearch(Student* pSt, char* name, int StdNum)
{
Student* find = 0;
for (int i = 0; i < StdNum; i++)
{
if (strcmp((pSt + i)->StdName, name) == 0)
{
find = pSt + i;
return find;
}
}
if (find == 0)
return NULL;
}
我正在编写 c++ 代码并开始使用“strcmp”命令,但我不知道是否应该像 c 那样包含 string.h 库。
您应该包括#include <cstring>
以在 C++ 中使用strcmp
。 惯例是一些 C 标准头文件可以包含在 C++ 中,方法是删除.h
并在c
之前添加<cstdio>
cstlib <cstdlib>
<cstdint>
等。
为什么没有出现错误?
允许但不要求标准标头包含它们自己。 因此,您可以执行#include <iostream>
并且iostream
包含cstring
或类似内容。 它现在可以在您的计算机上运行,但可能会因更新或在其他人的计算机上而失败。 为确保符号可见,请在符号中包含 header。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.