简体   繁体   English

c 和 c++ 使用同一个库吗?

[英]Do c and c++ use the same library?

  1. I was writing a c++ code and came to use the 'strcmp' command, but I don't know if I should include the string.h library as in c.我正在编写 c++ 代码并开始使用“strcmp”命令,但我不知道是否应该像 c 那样包含 string.h 库。

  2. The c++ code used 'strcmp' without including the string.h library, but there was no error. c++ 代码使用 'strcmp' 不包括 string.h 库,但没有错误。 Why didn't there be an error?为什么没有出现错误?

    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;
    }

I was writing a c++ code and came to use the 'strcmp' command, but I don't know if I should include the string.h library as in c.我正在编写 c++ 代码并开始使用“strcmp”命令,但我不知道是否应该像 c 那样包含 string.h 库。

You should include #include <cstring> to use strcmp in C++.您应该包括#include <cstring>以在 C++ 中使用strcmp The convention is that some C standard headers can be included into C++ by removing .h and prepending c like <cstdio> <cstdlib> <cstdint> etc.惯例是一些 C 标准头文件可以包含在 C++ 中,方法是删除.h并在c之前添加<cstdio> cstlib <cstdlib> <cstdint>等。

Why didn't there be an error?为什么没有出现错误?

It is allowed, but not required , that standard headers include themselves.允许但不要求标准标头包含它们自己。 So it can be that you do #include <iostream> and iostream includes cstring or similar.因此,您可以执行#include <iostream>并且iostream包含cstring或类似内容。 It may work on your computer right now, but may fail with an update or on someone else's pc.它现在可以在您的计算机上运行,但可能会因更新或在其他人的计算机上而失败。 To be sure that the symbol is visible, include the header with the symbol.为确保符号可见,请在符号中包含 header。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM