Skip to content

Commit 5b1c507

Browse files
extern_c.cpp
1 parent d398e54 commit 5b1c507

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

extern_c.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <stdio.h>
2+
3+
//https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c
4+
//https://stackoverflow.com/questions/25210629/what-does-this-nm-output-mean
5+
6+
extern "C" void show(const char* s){
7+
printf("%s\n", s);
8+
};
9+
10+
//extern "C" suppresses mangling, so overloading can't be performed
11+
//error: conflicting declaration of C function ‘void show(double)’
12+
/*
13+
extern "C" void show(double d){
14+
printf("%lf\n", d);
15+
};
16+
*/
17+
18+
//without extern "C", we can do overloading
19+
void show(int i){
20+
printf("%d\n", i);
21+
};
22+
23+
void show(float f){
24+
printf("%f\n", f);
25+
};
26+
27+
int main()
28+
{
29+
const char* s = "GeeksforGeeks";
30+
int i = 3;
31+
float f = 3.5;
32+
show(s);
33+
show(i);
34+
show(f);
35+
return 0;
36+
}
37+
38+
/*
39+
output from "nm a.out | grep show":
40+
00000000004005e6 T show
41+
0000000000400623 T _Z4showf
42+
0000000000400601 T _Z4showi
43+
44+
We can see that the name of the function show declared using extern "C" is not mangled.
45+
*/

0 commit comments

Comments
 (0)