简体   繁体   中英

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.

  2. The c++ code used 'strcmp' without including the string.h library, but there was no error. 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.

You should include #include <cstring> to use strcmp in C++. The convention is that some C standard headers can be included into C++ by removing .h and prepending c like <cstdio> <cstdlib> <cstdint> etc.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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