{"id":2942,"date":"2016-07-25T13:08:20","date_gmt":"2016-07-25T13:08:20","guid":{"rendered":"https:\/\/moodle.sit.ac.in\/wp\/?st-import=8ec6fc6fca0363b702b2f4b8be2850e3"},"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\/wp\/2016\/07\/25\/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 fetchpriority=\"high\" decoding=\"async\" class=\" size-full wp-image-77 aligncenter\" src=\"https:\/\/moodle.sit.ac.in\/wp\/wp-content\/uploads\/2016\/07\/lifecycle.jpg\" alt=\"LifeCycle\" width=\"1056\" height=\"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":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2942","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry"],"_links":{"self":[{"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/posts\/2942","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/comments?post=2942"}],"version-history":[{"count":0,"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/posts\/2942\/revisions"}],"wp:attachment":[{"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/media?parent=2942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/categories?post=2942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moodle.sit.ac.in\/wp\/wp-json\/wp\/v2\/tags?post=2942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}