{"id":694,"date":"2014-06-07T00:01:39","date_gmt":"2014-06-07T07:01:39","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=694"},"modified":"2014-08-03T10:05:35","modified_gmt":"2014-08-03T17:05:35","slug":"the-joy-of-clang","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=694","title":{"rendered":"The Joy of Clang"},"content":{"rendered":"<p>If it were spelled Klang, then it might be a reference some Klingon character from <em>Star Trek<\/em>, but it&#8217;s not. Clang is one of those program names that&#8217;s a contraction. In this instance, it&#8217;s a portmanteau of <em>C Language<\/em>. It&#8217;s an \u00fcber nifty command line compiler.<br \/>\n<!--more--><br \/>\nIn my most recent C programming book, <em>Beginning Programming with C For Dummies<\/em>, I focus on the IDE method of program creation, specifically by using the <a href=\"http:\/\/www.codeblocks.org\/\" target=\"_blank\">Code::Blocks<\/a> environment. That&#8217;s pretty much the way all the cool kids are programming today.<\/p>\n<p>For the small programs introduced in my books, however, the command line works just fine. In fact, if you&#8217;re familiar with the command line, it can be easier to use than an IDE for those small programs.<\/p>\n<blockquote><p>I use the command line to compose all the sample code in this blog.<\/p><\/blockquote>\n<p>For years, the <code>cc<\/code> compiler, or the GNU version <code>gcc<\/code>, was preferred for compiling C code. After all, <code>cc<\/code>, was the original C Compiler developed by Kernighan and Ritchie.<\/p>\n<p>Today, a better solution is <code>clang<\/code>, the C language compiler. Unlike <code>gcc<\/code>, it doesn&#8217;t attempt to compile everything, just C code. And it does a super spiffy job.<\/p>\n<blockquote><p>On many Unix systems, <code>cc<\/code> is aliased to the <code>clang<\/code> compiler, so using <code>cc<\/code> or <code>clang<\/code> to compile is the same thing.<\/p><\/blockquote>\n<p>First, a tutorial:<\/p>\n<p>To compile on the command line, you need a text editor and a compiler\/linker. The routine works like this:<\/p>\n<ol>\n<li><strong>Use a text editor to create your code.<\/strong> I use <code>vim<\/code>, the <em>vi improved<\/em> editor, although Unix is bursting at the seams with text editors various and sundry.<\/li>\n<li><strong>Compile and link the code.<\/strong> The <code>cc<\/code> command is followed by the source code filename. That command both compiles and links, assuming no errors are found.<\/li>\n<li><strong>Run the program.<\/strong> Type the program&#8217;s name at the prompt to see how it works.<\/li>\n<\/ol>\n<p>As an example, suppose you wanted to compose the following code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for(x=65;x&lt;65+26;x++);\r\n        printf(\"%c\",x);\r\n    putchar('\\n');\r\n\r\n    return(0);\r\n}<\/pre>\n<p>This code contains a deliberate error &#8212; a common programming mistake, but not one that generates a warning when compiled with <code>cc<\/code>. If you can&#8217;t find the problem (and most programmers won&#8217;t, especially in their own code), then <code>clang<\/code> will.<\/p>\n<p>Assume the source code filename is <code>0607.c<\/code>. The code is compiled with this command:<\/p>\n<pre><code>clang 0607.c<\/code><\/pre>\n<p>Figure 1 shows a screen cap of the output:<\/p>\n<div id=\"attachment_738\" style=\"width: 595px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-738\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/blog-0607.fig1_.png\" alt=\"Figure 1. Compiling bugging code by using clang.\" width=\"585\" height=\"160\" class=\"size-full wp-image-738\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/blog-0607.fig1_.png 585w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/blog-0607.fig1_-300x82.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2014\/05\/blog-0607.fig1_-500x136.png 500w\" sizes=\"auto, (max-width: 585px) 100vw, 585px\" \/><p id=\"caption-attachment-738\" class=\"wp-caption-text\">Figure 1. Compiling bugging code by using <code>clang<\/code>.<\/p><\/div>\n<p>As you can see, <code>clang<\/code> dutifully spotted the errant semicolon &#8212; a bane of all programmers. It specifically pointed out not only the line, but its assumption of what was wrong. Further, it offered a suggestion on how to avoid the warning in the future, should this condition be intentional.<\/p>\n<p>Wow.<\/p>\n<p><code>clang<\/code> was developed by Apple and Google several years back, designed to be perfectly compatible with <code>cc<\/code>. If you&#8217;re using <code>gcc<\/code> to compile a terminal window, switch to <code>clang<\/code>. You can determine whether or not the system has <code>clang<\/code> installed by typing:<\/p>\n<pre><code>clang --version<\/code><\/pre>\n<p>at the command prompt.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Forget <code>gcc<\/code>. Banish <code>cc<\/code>. What you need to compile on the command line is <code>clang<\/code>. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=694\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-694","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/694","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=694"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/694\/revisions"}],"predecessor-version":[{"id":930,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/694\/revisions\/930"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=694"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=694"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=694"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}