Search code examples
c++linkage

What does this External Linkage directive mean?


Can someone explain this note in C++ primer 5th edition to me:

Note

The functions that C++ inherits from the C library are permitted to be defined as C functions but are not required to be C functions—it’s up to each C++ implementation to decide whether to implement the C library functions in C or C++.


Solution

  • I will give you an example of one function, that could be "treated" as C or C++. std::toupper() can also be written as toupper(). I believe the first one uses some safety checks of C++, while the other one is strictly C. But what it boils down to is:

    #include <cctype>
    #include <iostream>
    
    int main (void) {
        char c = 'b';
    
        std::cout << toupper(c);                                                                     
    
        return 0;
    }
    

    compiles using C way, while:

    #include <cctype>
    #include <iostream>
    
    int main (void) {
        char c = 'b';
    
        std::cout << std::toupper(c);                                                                     
    
        return 0;
    }
    

    Uses C++ compilation addressing namespaces, ant that is what @numzero's answer talks about.

    Now both will compile, but it is up to you and your own risk to use C function.