Name Mangling in C and C++

Name Mangling is a programming concept that is followed by many languages such as C++. This is a special kind of operation applied on entity names, in order to ensure there are unique names for every entity. If there is a not unique name for every entity, the compiler will have trouble due to ambiguity between entities.

Name mangling is only a necessity with languages that support Function Overloading. Those that do not support it, are not required to use Name Mangling. However, it is still used to a certain degree to give additional information about entity names.

What is Name Mangling in C++?

Name Mangling generates a unique name, by using additional information that the entity contains. For example, a function name. Now in C++, due to Function Overloading support, multiple functions can have the same name.

This would normally cause problems for the compiler when resolving names, but behind the scenes, C++ performs Name mangling to ensure that even functions with the same name end up with unique one.

Let’s take a look at the following example.

void func() {  cout << "Default\n";  }

void func(int n) {  cout << n << endl;  }

Now how does C++ generate names for these? Well, as we mentioned earlier, it does this by adding some information about the entity, that results in a unique name. In this case, it uses the argument types to generate a unique name. And since two function cannot have the same name as well as the same number and type of arguments, a unique name is always generated this way. (This is also why the number and type of parameters cannot be the same, otherwise a unique name will not be generated)

So from the above example, we get names that look something like this:

void __func_v()  {  cout << "Default\n";  }

void __func_i()  {  cout << n << endl;  }

Basically a character is appended to the function name for each argument it has. If there is no argument, then “v” is appended. For an integer argument, “i” is appended. Similarly, for character “c” is appended and so on.

The exact way that Name mangling is implemented varies from language to language (different characters may be used) and version to version. It’s not really important to know how it occurs, rather you should understand the concept, and “how” it works in theory.

Preventing Name Mangling with extern “C”

The most interesting example we can show you on Name Mangling, is actually how to prevent name mangling from occurring in C++.

Unlike C++, the C language does not support Function overloading, hence function names are not mangled. This means that the C compiler does not expect to received mangled names. Which also means that if it receives a mangled function name, it will not be able to handle it correctly.

In this example, we are going to try and use a function from C in a C++ program. We won’t be doing it the normal way however, which is to use the #include <stdio.h> statement. We are going to define the function declaration our selves. (Just the function declaration, not the definition)

The reason this we can actually do this, is because if we think about it, stdio.h is just a header file with function declarations, that link back to the function definitions. So if we just provide the function declaration ourselves, then there won’t be any problems.

// CPP Program 
int printf(const char* format, ...);

int main()  {
    printf("Hello World");
    return 0;
error: undefined reference to `printf(char const*, ...)'

As you can see, the above code returns an error. Why though? It’s not because we made the wrong declaration, rather the problem is, that this is a C function. Our C++ program is performing name mangling on the C function declaration, which is causing an error.

Luckily, we can easily fix this using the extern “C” keyword as shown below.

extern "C" {
int printf(const char* format, ...);
int main()  {
    printf("Hello World");
    return 0;
Hello World

The reason for why this is working, is because extern “C” prevents name mangling from taking place on any functions within it’s curly brackets. Due to this, the function is called correctly.

This marks the end of the C++ Name Mangling Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.