{"id":5650,"date":"2019-03-08T09:13:34","date_gmt":"2019-03-08T09:13:34","guid":{"rendered":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-mixing-c-with-c\/"},"modified":"2023-06-26T10:14:12","modified_gmt":"2023-06-26T10:14:12","slug":"c-core-guidelines-mixing-c-with-c","status":"publish","type":"post","link":"https:\/\/www.modernescpp.com\/index.php\/c-core-guidelines-mixing-c-with-c\/","title":{"rendered":"C++ Core Guidelines: Mixing C with C++"},"content":{"rendered":"<p>Due to the same history of C and C++, both languages are closely related. Because neither of them is a subset of the other, you have to know a few rules to mix them.<\/p>\n<p><!--more--><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5644\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/blender-10935_1280.jpg\" alt=\"blender 10935 1280\" width=\"400\" height=\"400\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/blender-10935_1280.jpg 1280w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/blender-10935_1280-300x300.jpg 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/blender-10935_1280-1024x1024.jpg 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/blender-10935_1280-150x150.jpg 150w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/blender-10935_1280-768x768.jpg 768w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>The chapter in the C++ core guidelines is called: C-style programming. Honestly, my first thought was to skip it, but after more thought, I decided to write about it. My reason is twofold:<\/p>\n<ol>\n<li>These are the typical issues we have when dealing with legacy code.<\/li>\n<li>One reader wanted that I write more about the challenges of legacy code.<\/li>\n<\/ol>\n<p>Here are the three rules for today:<\/p>\n<ul>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rcpl-C\">CPL.1: Prefer C++ to C<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rcpl-subset\">CPL.2: If you must use C, use the common subset of C and C++, and compile the C code as C++<\/a><\/li>\n<li><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rcpl-interface\">CPL.3: If you must use C for interfaces, use C++ in the calling code using such interfaces<\/a><\/li>\n<\/ul>\n<p>The first rules are evident because I write about the C++ core guidelines.<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rcpl-C\">CPL.1: Prefer C++ to C<\/a><\/h2>\n<p>&nbsp;The reason from the C++ core guidelines: &#8220;C++ provides better type checking and more notational support. It provides better support for high-level programming and often generates faster code.&#8221;<\/p>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rcpl-subset\">CPL.2: If you must use C, use the common subset of C and C++, and compile the C code as C++<\/a><\/h2>\n<p>The first question you must answer is: Can you compile the entire code with a C++ compiler?<\/p>\n<h3>The entire source code is available<\/h3>\n<p>Fine, you are almost done. Almost, because C is not a subset of C++. Here is a small and inadequate C program that will break with a C++ compiler.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ cStyle.c<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;stdio.h&gt;<\/span>\r\n\r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>(){\r\n\r\n    <span style=\"color: #007788; font-weight: bold;\">double<\/span> sq2 <span style=\"color: #555555;\">=<\/span> sqrt(<span style=\"color: #ff6600;\">2<\/span>);                           <span style=\"color: #0099ff; font-style: italic;\">\/\/ (1)<\/span>\r\n    \r\n    printf(<span style=\"color: #cc3300;\">\"<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n<\/span><span style=\"color: #cc3300;\">sizeof(<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\'<\/span><span style=\"color: #cc3300;\">a<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\'<\/span><span style=\"color: #cc3300;\">): %d<\/span><span style=\"color: #cc3300; font-weight: bold;\">\\n\\n<\/span><span style=\"color: #cc3300;\">\"<\/span>, <span style=\"color: #006699; font-weight: bold;\">sizeof<\/span>(<span style=\"color: #cc3300;\">'a'<\/span>)); <span style=\"color: #0099ff; font-style: italic;\">\/\/ (2)<\/span>\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">char<\/span> c;\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span><span style=\"color: #555555;\">*<\/span> pv <span style=\"color: #555555;\">=<\/span> <span style=\"color: #555555;\">&amp;<\/span>c;\r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span><span style=\"color: #555555;\">*<\/span> pi <span style=\"color: #555555;\">=<\/span> pv;                                   <span style=\"color: #0099ff; font-style: italic;\">\/\/ (3)<\/span>\r\n    \r\n    <span style=\"color: #007788; font-weight: bold;\">int<\/span> class <span style=\"color: #555555;\">=<\/span> <span style=\"color: #ff6600;\">5<\/span>;                                  <span style=\"color: #0099ff; font-style: italic;\">\/\/ (4)<\/span>\r\n    \r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>First, let me compile and execute it with the C90 standard.<\/p>\n<p>The compilation succeeds with a few warnings.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5645\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleC2.png\" alt=\"cStyleC2\" width=\"700\" height=\"229\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleC2.png 1071w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleC2-300x98.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleC2-1024x336.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleC2-768x252.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>The program<span style=\"font-family: courier new, courier;\"> cStyle.c<\/span> has a few issues. There is no declaration for the <span style=\"font-family: 'courier new', courier;\">sqrt<\/span> function (line 2), line (3) performs an implicit conversion from a <span style=\"font-family: 'courier new', courier;\">void<\/span> pointer to an <span style=\"font-family: courier new, courier;\">int<\/span> pointer, and line (4) uses the keyword <span style=\"font-family: courier new, courier;\">class.<\/span><\/p>\n<p>Let&#8217;s see what the C++ compiler is saying.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5646\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCpp.png\" alt=\"cStyleCpp\" width=\"700\" height=\"231\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCpp.png 1044w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCpp-300x99.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCpp-1024x338.png 1024w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCpp-768x254.png 768w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/p>\n<p>I get what I deserve: three compiler errors. The program <span style=\"font-family: courier new, courier;\">cStyle.c<\/span> shows are more subtle differences between a C and a C++ compiler.&nbsp; I reduced the program to line (2): <span style=\"font-family: courier new, courier;\">printf(&#8220;\\nsizeof(\\&#8217;a\\&#8217;): %d\\n\\n&#8221;, sizeof(&#8216;a&#8217;));.<\/span>&nbsp;Here is the output.<span style=\"font-family: courier new, courier;\"><\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5647\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCppSizeof.png\" alt=\"cStyleCppSizeof\" width=\"400\" height=\"179\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCppSizeof.png 469w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/cStyleCppSizeof-300x134.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>Instead of 4, such as for the C compiler, sizeof<span style=\"font-family: courier new, courier;\">(&#8216;a&#8217;)<\/span> is 1 with the C++ compiler.<strong> <span style=\"font-family: courier new, courier;\">&#8216;c&#8217;<\/span> is an<span style=\"font-family: 'courier new', courier;\"> int<\/span> in C.<\/strong><\/p>\n<p>Now, to the more challenging job.<\/p>\n<h3>Entire source code is not available<\/h3>\n<p>These are the critical points.<\/p>\n<ol>\n<li><strong>Use your C++ compiler to compile your <span style=\"font-family: 'courier new', courier;\">main<\/span> function.<\/strong> In contrast to a C compiler, a C++ compiler generates additional startup code executed before the <span style=\"font-family: courier new, courier;\">main<\/span> function. For example, this startup code calls constructors of global (static) objects.&nbsp;&nbsp;<\/li>\n<li><strong>Use your C++ compiler to link your program.<\/strong> The C<span style=\"font-family: courier new, courier;\">++<\/span> compiler, when used for linking the program, will automatically link in the standard C++ libraries.<\/li>\n<li><strong>Use a C and C++ compiler from the same vendor with the same calling conventions.<\/strong> A calling convention specifies a compiler&#8217;s method to access a function. This includes in which order parameters are allocated, how parameters are passed, or whether the caller of the callee prepares the stack. Read the full details of x86&#8217;s calling conventions on <a href=\"https:\/\/en.wikipedia.org\/wiki\/X86_calling_conventions\">Wikipedia.<\/a><\/li>\n<\/ol>\n<h2><a href=\"http:\/\/isocpp.github.io\/CppCoreGuidelines\/CppCoreGuidelines#Rcpl-interface\">CPL.3: If you must use C for interfaces, use C++ in the calling code using such interfaces<\/a><\/h2>\n<p>In contrast to C, C++ supports function overloading. This means you can define a function with the same name but different parameters. The compiler picks the correct function when a function is invoked.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0099ff; font-style: italic;\">\/\/ functionOverloading.cpp<\/span>\r\n\r\n<span style=\"color: #009999;\">#include &lt;iostream&gt;  <\/span>\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">print<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>) { \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"int\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n} \r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">print<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>) { \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"double\"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n} \r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">print<\/span>(<span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span>) { \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"const char* \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n} \r\n\r\n<span style=\"color: #007788; font-weight: bold;\">void<\/span> <span style=\"color: #cc00ff;\">print<\/span>(<span style=\"color: #007788; font-weight: bold;\">int<\/span>, <span style=\"color: #007788; font-weight: bold;\">double<\/span>, <span style=\"color: #006699; font-weight: bold;\">const<\/span> <span style=\"color: #007788; font-weight: bold;\">char<\/span><span style=\"color: #555555;\">*<\/span>) { \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> <span style=\"color: #cc3300;\">\"int, double, const char* \"<\/span> <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl; \r\n} \r\n\r\n  \r\n<span style=\"color: #007788; font-weight: bold;\">int<\/span> <span style=\"color: #cc00ff;\">main<\/span>() { \r\n \r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;   \r\n\r\n    print(<span style=\"color: #ff6600;\">10<\/span>); \r\n    print(<span style=\"color: #ff6600;\">10.10<\/span>); \r\n    print(<span style=\"color: #cc3300;\">\"ten\"<\/span>); \r\n    print(<span style=\"color: #ff6600;\">10<\/span>, <span style=\"color: #ff6600;\">10.10<\/span>, <span style=\"color: #cc3300;\">\"ten\"<\/span>);\r\n\r\n    std<span style=\"color: #555555;\">::<\/span>cout <span style=\"color: #555555;\">&lt;&lt;<\/span> std<span style=\"color: #555555;\">::<\/span>endl;\r\n\r\n}\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>The output is as expected.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5648\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/functionOverloading.png\" alt=\"functionOverloading\" width=\"350\" height=\"229\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/functionOverloading.png 394w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/functionOverloading-300x196.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>The exciting question is how the C++ compiler can distinguish the various functions. The C++ compiler encodes the type of the parameters additionally into the function name. This process is called name mangling and is specific for each C++ compiler. The process which is not standardized is often also called name decoration.<\/p>\n<p>With the help of the <a href=\"https:\/\/godbolt.org\/z\/gFn4NU\"><span style=\"font-family: courier new, courier;\">functionOverloading.cpp<\/span><\/a> <a href=\"https:\/\/godbolt.org\/z\/gFn4NU\">on compiler explorer<\/a>, it is pretty easy to show the mangled name. Just disable the button Demangle.<\/p>\n<p>Here are the names that the GCC 8.3 and MSVC 19.16 is producing.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" size-full wp-image-5649\" src=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/Mangling.png\" alt=\"Mangling\" width=\"600\" height=\"131\" style=\"display: block; margin-left: auto; margin-right: auto;\" srcset=\"https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/Mangling.png 1019w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/Mangling-300x65.png 300w, https:\/\/www.modernescpp.com\/wp-content\/uploads\/2019\/03\/Mangling-768x167.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>By using the extern &#8220;C&#8221; linkage specifier, you can prevent the C++ compiler from mangling the names.<\/p>\n<p>By declaring the function with <span style=\"font-family: courier new, courier;\">extern &#8220;C&#8221;<\/span> in your code, you can call a C function from C++ or a C++ function from C.<\/p>\n<p>You can use <span style=\"font-family: courier new, courier;\">extern &#8220;C&#8221;<\/span> for each function,<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">extern<\/span> <span style=\"color: #cc3300;\">\"C\"<\/span> <span style=\"color: #007788; font-weight: bold;\">void<\/span> foo(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);\r\n<\/pre>\n<\/div>\n<p>for each function in a scope,<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #006699; font-weight: bold;\">extern<\/span> <span style=\"color: #cc3300;\">\"C\"<\/span> {\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> foo(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);\r\n    <span style=\"color: #007788; font-weight: bold;\">double<\/span> <span style=\"color: #cc00ff;\">bar<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>);\r\n};\r\n<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>or for the entire header file by using include guards. The macro<span style=\"font-family: courier new, courier;\"> __cplusplus<\/span> is defined when the C++ compiler is used.<\/p>\n<div style=\"background: #f0f3f3; overflow: auto; width: auto; gray;border-width: .1em .1em .1em .8em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #009999;\">#ifdef __cplusplus<\/span>\r\n<span style=\"color: #006699; font-weight: bold;\">extern<\/span> <span style=\"color: #cc3300;\">\"C\"<\/span> {\r\n<span style=\"color: #009999;\">#endif<\/span>\r\n    <span style=\"color: #007788; font-weight: bold;\">void<\/span> foo(<span style=\"color: #007788; font-weight: bold;\">int<\/span>);\r\n    <span style=\"color: #007788; font-weight: bold;\">double<\/span> <span style=\"color: #cc00ff;\">bar<\/span>(<span style=\"color: #007788; font-weight: bold;\">double<\/span>);\r\n    .\r\n    .\r\n    .\r\n<span style=\"color: #009999;\">#ifdef __cplusplus<\/span>\r\n}\r\n<span style=\"color: #009999;\">#endif<\/span>\r\n<\/pre>\n<\/div>\n<h2>What&#8217;s next?<\/h2>\n<p>I&#8217;m pleased to announce that the<a href=\"https:\/\/www.modernescpp.com\/index.php\/c-insights-conversions\"> next post <\/a>begins a series to <a href=\"https:\/\/cppinsights.io\/\">CppInsight<\/a>. CppInsight is an incredible tool that I use heavily in my posts and classes to show the magic of the C++ compiler. But the tool lacks a good introduction. Who can be better equipped for writing this introduction as Andreas Fertig, the author of CppInsight?<\/p>\n<p>&nbsp;<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Due to the same history of C and C++, both languages are closely related. Because neither of them is a subset of the other, you have to know a few rules to mix them.<\/p>\n","protected":false},"author":21,"featured_media":5644,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[372],"tags":[475],"class_list":["post-5650","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-modern-c","tag-c"],"_links":{"self":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/comments?post=5650"}],"version-history":[{"count":1,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5650\/revisions"}],"predecessor-version":[{"id":6795,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/posts\/5650\/revisions\/6795"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media\/5644"}],"wp:attachment":[{"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/media?parent=5650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/categories?post=5650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.modernescpp.com\/index.php\/wp-json\/wp\/v2\/tags?post=5650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}