{"id":2309,"date":"2024-11-06T10:38:33","date_gmt":"2024-11-06T10:38:33","guid":{"rendered":"https:\/\/itnotes.apjsoftwares.in\/?p=2309"},"modified":"2024-11-06T10:38:33","modified_gmt":"2024-11-06T10:38:33","slug":"compiling-a-c-program-behind-the-scenes","status":"publish","type":"post","link":"https:\/\/itnotes.apjsoftwares.in\/index.php\/2024\/11\/06\/compiling-a-c-program-behind-the-scenes\/","title":{"rendered":"Compiling a C Program: Behind the Scenes"},"content":{"rendered":"\n<p>The compilation is the process of converting the source code of the C language into machine code. As C is a mid-level language, it needs a compiler to convert it into an executable code so that the program can be run on our machine.<\/p>\n\n\n\n<p>The C program goes through the following phases during compilation:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20230404112946\/Compilation-Process-in-C.png\" alt=\"compilation process in c\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How do we compile and run a C program?<\/strong><\/h2>\n\n\n\n<p>We first need a compiler and a code editor to compile and run a C Program. The below example is of an Ubuntu machine with GCC compiler.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Creating a C Source File<\/h3>\n\n\n\n<p>We first create a C program using an editor and save the file as filename.c<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong> $ vi filename.c<\/strong><\/pre>\n\n\n\n<p>We can write a simple hello world program and save it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Compiling using GCC compiler<\/h3>\n\n\n\n<p>We use the following command in the terminal for compiling our filename.c source file<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong> $ gcc filename.c \u2013o filename<\/strong><\/pre>\n\n\n\n<p>We can pass many instructions to the GCC compiler to different tasks such as:<\/p>\n\n\n\n<ul>\n<li>The option -Wall enables all compiler\u2019s warning messages. This option is recommended to generate better code.&nbsp;<\/li>\n\n\n\n<li>The option -o is used to specify the output file name. If we do not use this option, then an output file with the name a.out is generated.<\/li>\n<\/ul>\n\n\n\n<p>If there are no errors in our C program, the executable file of the C program will be generated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Executing the program<\/h3>\n\n\n\n<p>After compilation executable is generated and we run the generated executable using the below command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong> $ .\/filename<\/strong><\/pre>\n\n\n\n<p>The program will be executed and the output will be shown in the terminal.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/media.geeksforgeeks.org\/wp-content\/uploads\/20230406112620\/c-source-file-compliled-and-executed.webp\" alt=\"c file compilation and execution\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>The compilation is the process of converting the source code of the C language into&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[],"_links":{"self":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/2309"}],"collection":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/comments?post=2309"}],"version-history":[{"count":1,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/2309\/revisions"}],"predecessor-version":[{"id":2310,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/posts\/2309\/revisions\/2310"}],"wp:attachment":[{"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/media?parent=2309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/categories?post=2309"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itnotes.apjsoftwares.in\/index.php\/wp-json\/wp\/v2\/tags?post=2309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}