{"id":751,"date":"2020-10-22T13:27:15","date_gmt":"2020-10-22T13:27:15","guid":{"rendered":"https:\/\/linuxways.net\/?p=751"},"modified":"2020-10-22T16:23:45","modified_gmt":"2020-10-22T16:23:45","slug":"how-to-create-and-run-a-c-program-using-ubuntu-20-04-lts","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-create-and-run-a-c-program-using-ubuntu-20-04-lts\/","title":{"rendered":"How to create and run a C program using Ubuntu 20.04 LTS"},"content":{"rendered":"<p><\/p>\n\n\n<p>Linux is an operating system. It has been around since the mid-1990s. Over time its user base has expanded to all across the globe. Today, it can be found everywhere, from your phones, cars, to your TVs and refrigerators.\u00a0<\/p>\n<p>Ubuntu is a distribution of Linux. In this article, we will go over how to compile and run the traditional Hello World! program in C on Ubuntu 20.04. Follow the steps shown below to create your very first C program.<\/p>\n<h2>Opening the terminal<\/h2>\n<p>Click the <strong>Show<\/strong> <strong>Applications<\/strong> icon. This icon can be found at the bottom right of the launcher bar as shown in the image below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"579\" class=\"wp-image-787\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-405.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-405.png 801w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-405-300x217.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-405-768x555.png 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/p>\n<p>This will take you to the <strong>Applications Menu<\/strong>, as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"793\" height=\"593\" class=\"wp-image-788\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-406.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-406.png 793w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-406-300x224.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-406-768x574.png 768w\" sizes=\"auto, (max-width: 793px) 100vw, 793px\" \/><\/p>\n<p>Type <strong>terminal<\/strong> in the search bar.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"601\" class=\"wp-image-789\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-407.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-407.png 802w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-407-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-407-768x576.png 768w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/p>\n<p>Select the <strong>terminal icon<\/strong> to launch it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"801\" height=\"602\" class=\"wp-image-790\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-408.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-408.png 801w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-408-300x225.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-408-768x577.png 768w\" sizes=\"auto, (max-width: 801px) 100vw, 801px\" \/><\/p>\n<h2>Checking if GCC is installed<\/h2>\n<p>The GNU Compiler Collection (GCC) is a collection of libraries and compilers for C and other languages like C++, Go, etc.<\/p>\n<p>To check if you have the GCC compiler installed on your system, run the following command.<\/p>\n<pre>gcc \u2013v<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"498\" height=\"60\" class=\"wp-image-791\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-409.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-409.png 498w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-409-300x36.png 300w\" sizes=\"auto, (max-width: 498px) 100vw, 498px\" \/><\/p>\n<p>If you get a similar message at the end of the following screenshot, then your GCC Compiler is installed and you can move on to creating your source code file.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"729\" height=\"439\" class=\"wp-image-792\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-410.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-410.png 729w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-410-300x181.png 300w\" sizes=\"auto, (max-width: 729px) 100vw, 729px\" \/><\/p>\n<p>If you see the message shown in the image below, then you need to install the GCC Compiler.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"513\" height=\"107\" class=\"wp-image-793\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-411.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-411.png 513w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-411-300x63.png 300w\" sizes=\"auto, (max-width: 513px) 100vw, 513px\" \/><\/p>\n<p>Use the command shown below to install the GCC compiler on your system. <strong><a href=\"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-gcc-on-ubuntu-20-04-lts\/\">A detailed article about GCC installation on Ubuntu 20.04 is here.<\/a><\/strong><\/p>\n<pre>sudo apt install gcc<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"522\" height=\"62\" class=\"wp-image-794\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-412.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-412.png 522w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-412-300x36.png 300w\" sizes=\"auto, (max-width: 522px) 100vw, 522px\" \/><\/p>\n<p>The first step is entering your password. Once you enter your password, the installation starts. You will next be asked to confirm the installation. Type in <strong>y<\/strong> to confirm the installation.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"492\" class=\"wp-image-795\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-413.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-413.png 726w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-413-300x203.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><\/p>\n<p>Once the installation completes successfully, you should see the following output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"713\" height=\"519\" class=\"wp-image-796\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-414.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-414.png 713w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-414-300x218.png 300w\" sizes=\"auto, (max-width: 713px) 100vw, 713px\" \/><\/p>\n<h2>Creating your C program<\/h2>\n<p>To create a file named \u201chello.c\u201d, enter the following command in your terminal. You can name your file whatever you want, but the best practice is to use a name descriptive of the program.<\/p>\n<pre>gedit hello.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"455\" height=\"26\" class=\"wp-image-797\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-415.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-415.png 455w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-415-300x17.png 300w\" sizes=\"auto, (max-width: 455px) 100vw, 455px\" \/><\/p>\n<p>When you run this command, Linux launches the text editor.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"809\" height=\"598\" class=\"wp-image-798\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-416.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-416.png 809w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-416-300x222.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-416-768x568.png 768w\" sizes=\"auto, (max-width: 809px) 100vw, 809px\" \/><\/p>\n<p>Enter the following C source code to print Hello World!<\/p>\n<pre>#include &lt;stdio.h&gt;<br \/><br \/>int main()<br \/><br \/>{ <br \/><br \/>printf(\"Hello World!\");<br \/><br \/>return 0;<br \/><br \/>}<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"600\" class=\"wp-image-799\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-417.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-417.png 806w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-417-300x223.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-417-768x572.png 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><\/p>\n<p>Save your file, close the text editor, and go back to the terminal.<\/p>\n<h2>A closer look at the Hello World! program<\/h2>\n<p>The first line of every C program begins with the pound sign, <strong>#<\/strong>. The first line is called the preprocessor directive. The purpose of the preprocessor directive is to instruct the compiler to do the required preprocessing before the actual compilation.<\/p>\n<p>Next up is the <strong>main()<\/strong> function. This function is the starting point of every C program and you can have only one main function in your program.<\/p>\n<p>Hello World! the example has two statements. A statement is an instruction in a program and it ends with a semicolon.<\/p>\n<p>The <strong>printf()<\/strong> function prints the output on the screen.<\/p>\n<p>Though the <strong>return 0;<\/strong> statement is not necessary for this program. The purpose of using it here is to help you get familiar with it as more complex programs need it.<\/p>\n<p>You may notice how the curly braces are in separate lines, this is not necessary. C is not fussy about how text is laid out. You could write the whole code in one line and it would still work, but it isn\u2019t the best practice. Using spaces and separate lines makes your code cleaner and easier to read.<\/p>\n<h2>Running your C program<\/h2>\n<p>There are two ways of running your source code, we will go through both here.<\/p>\n<h3>Method 1<\/h3>\n<p>Enter the command below<\/p>\n<pre>gcc \u2013o hello hello.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"714\" height=\"25\" class=\"wp-image-800\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-418.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-418.png 714w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-418-300x11.png 300w\" sizes=\"auto, (max-width: 714px) 100vw, 714px\" \/><\/p>\n<p>This command invokes the GCC compiler to compile the file hello.c. The result is outputted to hello, an executable. We used hello here, you can use any other word.<\/p>\n<p>The compiler creates a binary file if the source code has no errors.<\/p>\n<p>Enter the following command in the terminal next:<\/p>\n<pre>.\/hello<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"366\" height=\"24\" class=\"wp-image-801\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-419.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-419.png 366w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-419-300x20.png 300w\" sizes=\"auto, (max-width: 366px) 100vw, 366px\" \/><\/p>\n<p>This command loads the executable file into memory. This results in the CPU executing the instructions in it. The <strong>.\/<\/strong>\u00a0part of the command refers to the current directory. The <strong>.\/hello<\/strong>\u00a0command loads and runs your executable file\u00a0&#8216;hello&#8217;.<\/p>\n<p>If the program runs successfully, you should see the text, \u201cHello World!\u201d in your terminal as shown below.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"20\" class=\"wp-image-802\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-420.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-420.png 415w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-420-300x14.png 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/p>\n<h3>Method 2<\/h3>\n<p>Enter the command below<\/p>\n<pre>gcc hello.c<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"417\" height=\"40\" class=\"wp-image-803\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-421.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-421.png 417w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-421-300x29.png 300w\" sizes=\"auto, (max-width: 417px) 100vw, 417px\" \/><\/p>\n<p>This command generates a machine-executable bytecode file, \u201c<strong>a.out<\/strong>\u201d.<\/p>\n<p>You can view all the files by running the ls command<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"540\" height=\"63\" class=\"wp-image-804\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-422.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-422.png 540w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-422-300x35.png 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/p>\n<p>Next, run the following command<\/p>\n<pre>.\/a.out<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"413\" height=\"20\" class=\"wp-image-805\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-423.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-423.png 413w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-423-300x15.png 300w\" sizes=\"auto, (max-width: 413px) 100vw, 413px\" \/><\/p>\n<p>This command executes the bytecode and you will see the text \u201cHello World!\u201d on the terminal.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"393\" height=\"26\" class=\"wp-image-806\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-424.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-424.png 393w, https:\/\/linuxways.net\/wp-content\/uploads\/2020\/10\/word-image-424-300x20.png 300w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/p>\n<p>And there you go! You just wrote your first C program. Hello World! the program may seem useless and simple, but it is the best way to get started on learning how to program. By writing this yourself, you better under concepts that may otherwise seem abstract and vague.<\/p>","protected":false},"excerpt":{"rendered":"<p>Linux is an operating system. It has been around since the mid-1990s. Over time its user base has expanded to all across the globe. Today, it can be&hellip;<\/p>","protected":false},"author":5,"featured_media":781,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[42],"class_list":["post-751","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-c"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/751","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=751"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/751\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/781"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}