{"id":63,"date":"2016-07-25T13:08:20","date_gmt":"2016-07-25T13:08:20","guid":{"rendered":"https:\/\/prabodhcp.wordpress.com\/?p=63"},"modified":"2016-07-25T13:08:20","modified_gmt":"2016-07-25T13:08:20","slug":"life-cycle-of-a-c-program","status":"publish","type":"post","link":"https:\/\/moodle.sit.ac.in\/blog\/life-cycle-of-a-c-program\/","title":{"rendered":"Life Cycle of a C Program"},"content":{"rendered":"<p>Every C Program that is in text form goes through a series of transformations to obtain the final executable. The various Phases of this transformation are as follows:<\/p>\n<ol>\n<li>Preprocessing<\/li>\n<li>Assembling<\/li>\n<li>Compiling<\/li>\n<li>Linking<\/li>\n<li>Loading to main memory<\/li>\n<\/ol>\n<p>This Process is illustrated in the Figure 1 below<\/p>\n<figure id=\"attachment_77\" aria-describedby=\"caption-attachment-77\" style=\"width: 1056px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"size-full wp-image-77 aligncenter lazyload\" data-src=\"https:\/\/i0.wp.com\/moodle.sit.ac.in\/blog\/wp-content\/uploads\/2016\/07\/lifecycle.jpg?resize=1056%2C816&#038;ssl=1\" alt=\"LifeCycle\" width=\"1056\" height=\"816\" data-srcset=\"https:\/\/i0.wp.com\/moodle.sit.ac.in\/blog\/wp-content\/uploads\/2016\/07\/lifecycle.jpg?w=1056&amp;ssl=1 1056w, https:\/\/i0.wp.com\/moodle.sit.ac.in\/blog\/wp-content\/uploads\/2016\/07\/lifecycle.jpg?resize=300%2C232&amp;ssl=1 300w, https:\/\/i0.wp.com\/moodle.sit.ac.in\/blog\/wp-content\/uploads\/2016\/07\/lifecycle.jpg?resize=1024%2C791&amp;ssl=1 1024w, https:\/\/i0.wp.com\/moodle.sit.ac.in\/blog\/wp-content\/uploads\/2016\/07\/lifecycle.jpg?resize=768%2C593&amp;ssl=1 768w\" data-sizes=\"(max-width: 1000px) 100vw, 1000px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1056px; --smush-placeholder-aspect-ratio: 1056\/816;\" \/><figcaption id=\"caption-attachment-77\" class=\"wp-caption-text\">Figure 1<\/figcaption><\/figure>\n<hr \/>\n<p>First we will see how to execute a C program in a GNU\/Linux environment. We will be using the <a href=\"https:\/\/gcc.gnu.org\/\" target=\"_blank\" rel=\"noopener\">GCC\/GDB<\/a> toolchain that comes installed by default in almost all GNU\/Linux distributions.<\/p>\n<p>The following video link shows the different phases of the Life Cycle of a C program.<\/p>\n<p><strong><a href=\"https:\/\/youtu.be\/wEMZKROgsiU\" target=\"_blank\" rel=\"noopener\">Life Cycle of a C Program &#8211; Click to view<\/a><\/strong><\/p>\n<p>Consider the program given below that calculates the area of a Circle given its radius.<\/p>\n<p><code><br \/>\n\/\/ circle.c: Calculate and print the Areas of Circles<br \/>\n#include \/\/ Preprocessor directive<br \/>\n#define PI 3.1416<br \/>\n<\/code><br \/>\n<code><br \/>\ndouble fnCircularArea( double r ); \/\/ Function prototype<br \/>\n<\/code><br \/>\n<code><br \/>\nint main(void)<br \/>\n{<br \/>\ndouble dRadius = 1.0, dArea = 0.0;<br \/>\nprintf( \"\\tAreas of Circles\\n\\n\" );<br \/>\nprintf( \"\\t\\tRadius\\t\\tArea\\n\\t---------------------------------\\n\" );<br \/>\ndArea = fnCircularArea( dRadius );<br \/>\nprintf( \"Circle 1%14.3f\\t%15.5f\\n\", dRadius, dArea );<br \/>\ndRadius = 5.0;<br \/>\ndArea = fnCircularArea( dRadius );<br \/>\nprintf( \"Circle 2%14.3f\\t%15.5f\\n\", dRadius, dArea );<br \/>\nreturn 0;<br \/>\n}<br \/>\n<\/code><br \/>\n<code><br \/>\n\/\/ The function fnCircularArea( ) calculates the Area of a Circle<br \/>\n\/\/ Parameter: Radius of the Circle<br \/>\n\/\/ Returns: Area of the Circle<br \/>\ndouble fnCircularArea( double dR )<br \/>\n{<br \/>\nreturn PI * dR * dR;<br \/>\n}<br \/>\n<\/code><\/p>\n<p>Click on the following video link shows how to compile and execute the above C program.<\/p>\n<p><strong><a href=\"https:\/\/youtu.be\/JGW6FwMtgn0\" target=\"_blank\" rel=\"noopener\">Execution of a C program using gcc compiler &#8211; Click to view<\/a><\/strong><\/p>\n<p>TO check which version of gcc is installed<br \/>\n<code><br \/>\n$ gcc --version<br \/>\n<\/code><br \/>\nTo compile a program circle.c and to show all warnings<br \/>\n<code><br \/>\n$ gcc -Wall circle.c<br \/>\n<\/code><br \/>\nOutput Redirection<br \/>\n<code><br \/>\n$ gcc -Wall -o circle.x circle.c<br \/>\n<\/code><br \/>\nExecution<br \/>\n<code><br \/>\n$ .\/circle.x<br \/>\n<\/code><\/p>\n<p>Now we will see the different phases of transformation. The first phase is Preprocessing. Click on the below link to understand the phase of <strong>Preprocessing<\/strong>.<\/p>\n<p><strong><a href=\"https:\/\/youtu.be\/mHnZDFKxh1A\" target=\"_blank\" rel=\"noopener\">Preprocessing Phase &#8211; Click to view<\/a><\/strong><\/p>\n<p>Preprocess and write to circle.i<br \/>\n<code><br \/>\n$ gcc -E -o circle.i circle.c<br \/>\n<\/code><\/p>\n<p>Preprocess but do not remove comments<br \/>\n<code><br \/>\n$ gcc -E -C -o circle.i circle.c<br \/>\n<\/code><\/p>\n<p>In the next video link we will see how to perform assembling, compiling and linking.<\/p>\n<p><strong><a href=\"https:\/\/youtu.be\/tPvXbOOcsbQ\" target=\"_blank\" rel=\"noopener\">Assembling, Compiling and Linking &#8211; Click to view<\/a><\/strong><\/p>\n<p>During the assembling phase the preprocessed code is converted into assembly code.<\/p>\n<p>Assembly<br \/>\n<code><br \/>\n$ gcc -S circle.c<br \/>\n<\/code><\/p>\n<p>verbose mode<br \/>\n<code><br \/>\n$ gcc -S -fverbose-asm circle.c<br \/>\n<\/code><\/p>\n<p>During the Compilation stage the assembled code is converted into object code.<\/p>\n<p>Compile only<br \/>\n<code><br \/>\n$ gcc -c circle.c<br \/>\n<\/code><\/p>\n<p>The object code is then linked to get the final executable<br \/>\nLinking<br \/>\n<code><br \/>\n$ gcc circle.o -o circle.x<br \/>\n<\/code><\/p>\n<p>All the phases discussed above can be generated in a single command as follows<br \/>\n<code><br \/>\n$ gcc -Wall -o circle.x circle.c -save-temps<br \/>\n<\/code><\/p>\n<p>Finally take this quiz that can be accessed at this moodle site. Click on the following Link<br \/>\n<a href=\"http:\/\/moodle.sit.ac.in\/moodle\" target=\"_blank\" rel=\"noopener\"><strong> http:\/\/moodle.sit.ac.in\/moodle<\/strong><\/a><br \/>\nHere scroll down to find the course <strong>IITB OER QUIZ<\/strong><br \/>\nClick on it and login name as <strong>demouser\u00a0<\/strong> with <strong>Change@1nce<\/strong> as the password to access the course<br \/>\nThere in the course under Topic 1 you can find a <strong>Demo Quiz<\/strong>. Click and take the quiz<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every C Program that is in text form goes through a series of transformations to obtain the final executable. The various Phases of this transformation are as follows: Preprocessing Assembling Compiling Linking Loading to main memory This Process is illustrated in the Figure 1 below First we will see how to execute a C program [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[],"class_list":["post-63","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/posts\/63","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/comments?post=63"}],"version-history":[{"count":0,"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/posts\/63\/revisions"}],"wp:attachment":[{"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/media?parent=63"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/categories?post=63"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/blog\/wp-json\/wp\/v2\/tags?post=63"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}