{"id":10325,"date":"2025-09-01T20:17:53","date_gmt":"2025-09-01T18:17:53","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?page_id=10325"},"modified":"2025-09-01T21:35:58","modified_gmt":"2025-09-01T19:35:58","slug":"hello-c-2025","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/","title":{"rendered":"hello.c (2025)"},"content":{"rendered":"<p>This example is the written version of the first lesson with additional commentary that is intended to support you in going over the code that you built during the class.<\/p>\n<p>This example contains the following<\/p>\n<ul>\n<li>Part 1 of the hello.c code with linear flow<\/li>\n<li>Part 2 of the hello.c code with conditional flow<\/li>\n<li>Syntax and styling in C programs<\/li>\n<li>UML activity diagrams for both code examples<\/li>\n<li>Additional commentary on both programs<\/li>\n<\/ul>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#helloc_part_1_linear_flow\" >hello.c part 1: linear flow<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Algorithm_for_part_1\" >Algorithm for part 1<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Pseudo_code_for_part_1\" >Pseudo code for part 1<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#UML_activity_diagram_for_part_1\" >UML activity diagram for part 1<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Program_code\" >Program code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Syntax_and_styling\" >Syntax and styling<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Hello_part_2_conditional_flow\" >Hello part 2: conditional flow<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Activity_diagram\" >Activity diagram<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/#Program_code-2\" >Program code<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"helloc_part_1_linear_flow\"><\/span>hello.c part 1: linear flow<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In part one, we make a basic calculator to calculate the person&#8217;s age. It leaves in some bugs and shortcomings, some of which will be addressed in part 2 and some will need more knowledge in programming before they can be addressed, but it should be a good starting point.<\/p>\n<p>All examples contain additional commentary.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Algorithm_for_part_1\"><\/span>Algorithm for part 1<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Algorithms must explain the sequence of actions in a non-ambiguous and detailed manner. They can be represented in different notations. In this class, we mainly focus on UML activity diagrams, but occasionally we will also look at pseudo code. Today both examples will be given<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Pseudo_code_for_part_1\"><\/span>Pseudo code for part 1<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Pseudo code describes the steps taken in the algorithm, mixing both informal language and self-explanatory keywords, often used in\u00a0 various programming languages. This should be as human readable as possible, while still being structural. There are no formal rules for pseudo code.<\/p>\n<pre class=\"lang:c highlight:0 decode:true\">- Greet user\r\n- Ask for user's year of birth\r\n- Calculate the likely age of the user\r\n- Print the age<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"UML_activity_diagram_for_part_1\"><\/span>UML activity diagram for part 1<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>Activity diagrams are often used for visualizing the algorithm. They take longer to compose, but offer many benefits, including standardization and readability for non-developer positions.\u00a0 These are mainly used by large companies and governments, where specification quality, globally accepted certification and standardization are important.<\/p>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt1_en.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-10458\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt1_en.png\" alt=\"\" width=\"304\" height=\"362\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt1_en.png 499w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt1_en-252x300.png 252w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><\/a><\/p>\n<p>Activity diagrams are typically created programming language agnostic and human readable. In the diagram above, notice the following components<\/p>\n<ul>\n<li>The algorithm begins with an Initial node<\/li>\n<li>Arrows indicate control flow (the sequence of actions)<\/li>\n<li>Action nodes specify actions taken, but not details related to the programming language or formatting<\/li>\n<li>Algorithm ends with a final node, that has a comment describing the end condition<\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Program_code\"><\/span>Program code<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>In this code, you are introduced to the basic structure of a C program. Most programs start with a list of included libraries that provide sets of functions that can be used within the code. E.g. <span class=\"lang:c highlight:0 decode:true crayon-inline\">stdio.h<\/span>\u00a0 provides basic input and output functionality.\u00a0 Even though our program isn&#8217;t using it, adding\u00a0 <span class=\"lang:c highlight:0 decode:true crayon-inline\">math.h<\/span>\u00a0 library would provide access to mathematical functions and so on.<\/p>\n<p>This is followed by the <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function. C programs start executing code from the first statement in the <span class=\"lang:c highlight:0 decode:true crayon-inline\">main()<\/span>\u00a0 function and then continue executing code line-by-line. The main function ends with a <span class=\"lang:c highlight:0 decode:true crayon-inline \">return<\/span>\u00a0 statement, that terminates the function. In this example, it also terminates the program.<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">\/**\r\n * File:        hello.c\r\n * Author:      Risto Heinsar\r\n * Created:     01.09.2014\r\n * Edited:      23.07.2025\r\n *\r\n * Description: This is the first hello.c example showing program flow without \r\n *              any conditionals and basic input-output function calls.\r\n *\/\r\n\r\n\/\/ Includes the standard input-output library (printf, scanf)\r\n#include &lt;stdio.h&gt;\r\n\r\n\/\/ The program always starts from the main() function\r\nint main(void)\r\n{\r\n    \/\/ Print the greeting in the terminal.\r\n    printf(\"Hello!\\n\");\r\n    printf(\"This program determines the approximate age of a person\\n\\n\");\r\n\r\n    \/\/ Variable declaration, type int (integer), variable name \"year\"\r\n    int year;\r\n    \r\n    \/\/ Prompt the user and read the year of birth as integer\r\n    printf(\"Please enter your year of birth: \");\r\n    scanf(\"%d\", &amp;year);  \r\n    \r\n    \/\/ Calculating the rough estimate of the age\r\n    int age = 2025 - year; \r\n\r\n    \/\/ Printing the result\r\n    printf(\"You are most likely %d years of age\\n\", age);\r\n\r\n    \/\/ The program finished successfully. Integer 0 indicates success.\r\n    return 0; \r\n}<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Syntax_and_styling\"><\/span>Syntax and styling<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p><strong>Syntax sets the rules of the code that are required for it to work.<\/strong> This includes any structural elements set by the programming language in order for the computer to understand what you want to make it do. Most programming languages have different syntax rules, event though there is a lot of overlap (caused by influence of other programming languages, well proven practices etc). The same elements in one language can work differently in another language.<\/p>\n<p>From the example above, some notable ones syntactical elements<\/p>\n<ul>\n<li>Every statement ends with a semicolon<\/li>\n<li>All programs must have a <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function. Code starts executing form the <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function<\/li>\n<li>The statements within the <span class=\"lang:c highlight:0 decode:true crayon-inline \">main()<\/span>\u00a0 function are surrounded by opening <span class=\"lang:c highlight:0 decode:true crayon-inline \">{<\/span>\u00a0 and <span class=\"lang:c highlight:0 decode:true crayon-inline \">}<\/span>\u00a0 closing curly braces. We call the code in between these braces a\u00a0<strong>code block<\/strong>.\u00a0 This also specifies scope, which you will learn about later.<\/li>\n<li>All strings are surrounded by double quotes, i.e. <span class=\"lang:c highlight:0 decode:true crayon-inline\">&#8220;This is a string&#8221;<\/span><\/li>\n<li>Comments start with two forward slashes <span class=\"lang:c highlight:0 decode:true crayon-inline \">\/\/<\/span>\u00a0 (or can be surrounded with <span class=\"lang:c highlight:0 decode:true crayon-inline \">\/*<\/span>\u00a0 and <span class=\"lang:c highlight:0 decode:true crayon-inline \">*\/<\/span>\u00a0)<\/li>\n<li>Function calls are performed by stating the name of the function, followed by round parenthesis. Function calls we saw were to <span class=\"lang:c highlight:0 decode:true crayon-inline \">printf()<\/span>\u00a0 and <span class=\"lang:c highlight:0 decode:true crayon-inline \">scanf()<\/span>\n<ul>\n<li>You can pass arguments to the function within the parenthesis.<\/li>\n<li>If there are multiple arguments, they are separated by commas<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>Style is set by us, the programmers, for readability and maintainability of the code.<\/strong> Style does not affect whether the code can be compiled or executed, or how it performs. Most of the time, the style requirements are set by either the team you are working in or by the company you are working for. In this class, the style is set by the teachers of the class. You can check the style guide for more, however here are a few notes from the code above<\/p>\n<ul>\n<li>Variables are commonly declared right before they are needed<\/li>\n<li>Every file has a comment in the beginning stating the purpose of the file, author and date<\/li>\n<li>Empty lines are used between blocks of code to indicate different sections<\/li>\n<li>Comments are placed right before the code lines they are explaining. There is a space after the two forward slashes. Comments start with an upper case letter.<\/li>\n<li>Starting and ending curly brace for a block of code are aligned (same distance from the start of the line)<\/li>\n<li>Curly braces are put on separate lines<\/li>\n<li>The code within the code block (indicated by curly braces) is indented<\/li>\n<li>Commas in the argument list for functions are always followed by a space<\/li>\n<li>Variables are named using camelCase styling, where the first word starts with a lower case character and all other words start with an upper case character.<\/li>\n<li>Variable name must indicate the purpose of the variable (what it holds)<\/li>\n<li>All operators are preceded and followed by a space (e.g. mathematical operators such as +, -, \/, *. Equation operator =, comparison operators &lt;, &gt;, ==, !=,\u00a0 etc)<\/li>\n<li>Code line cannot exceed 80 characters. If it&#8217;s longer, it must be split over multiple lines.<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Hello_part_2_conditional_flow\"><\/span>Hello part 2: conditional flow<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In the second part, we introduce a conditional statement to account for whether the user&#8217;s birthday for the year has already passed or is it still upcoming. This helps to correct for the bug in the first part of the task, where we couldn&#8217;t exactly tell the age.<\/p>\n<p>Note, that while the results are now mostly accurate, there is still one small bug in the edge case. Can you spot it?<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Activity_diagram\"><\/span>Activity diagram<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>We base the solution on the first diagram. The modifications are following<\/p>\n<ul>\n<li>We need to know whether the user&#8217;s birthday is still upcoming in the calendar year or it&#8217;s already passed. We add it as an action statement.<\/li>\n<li>We add a conditional statement to use a different formula depending on whether the birthday has already occurred. This requires\n<ul>\n<li>a junction node to begin the conditional statement<\/li>\n<li>a merge node to finish the conditional statement<\/li>\n<li>a comment to specify the condition<\/li>\n<li>true\/false statements on control flows to specify the correct path depending on the condition<\/li>\n<\/ul>\n<\/li>\n<li>We also updated the print statement, as now we can determine the correct age in all but one case<\/li>\n<\/ul>\n<p><a href=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt2_en.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-10469\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt2_en.png\" alt=\"\" width=\"393\" height=\"481\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt2_en.png 754w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2025\/09\/hello_pt2_en-245x300.png 245w\" sizes=\"auto, (max-width: 393px) 100vw, 393px\" \/><\/a><\/p>\n<h4><span class=\"ez-toc-section\" id=\"Program_code-2\"><\/span>Program code<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The program code gets modified according to the specification by the algorithm.<\/p>\n<p>We add an additional prompt for information and a conditional statement that accounts for this information to calculate the correct age.<\/p>\n<p>Note the indentation of the code. The contents of the code block is always indented by 1. Since the if\/else statements contain code blocks that are already inside the main function&#8217;s code block, they get an additional indentation. This means that it&#8217;s very easy and quick to for us to glance over and understand the structure of the code, helping code maintainability.<\/p>\n<p>As mentioned before, there is one edge case, where this formula will be incorrect. Can you spot it?<\/p>\n<pre class=\"toolbar:2 lang:c decode:true\">\/**\r\n * File:        hello_conditional.c\r\n * Author:      Risto Heinsar\r\n * Created:     01.09.2014\r\n * Edited:      23.07.2025\r\n *\r\n * Description: This is the second hello example showing conditional program\r\n *              flow.\r\n *\/\r\n\r\n\/\/ Includes the standard input-output library (printf, scanf)\r\n#include &lt;stdio.h&gt;\r\n\r\n\/\/ The program always starts from the main() function\r\nint main(void)\r\n{\r\n    \/\/ Print the greeting in the terminal.\r\n    printf(\"Hello!\\n\");\r\n    printf(\"This program determines the approximate age of a person\\n\\n\");\r\n\r\n    \/\/ Variable declaration, type int (integer), variable name \"year\"\r\n    int year;\r\n    \r\n    \/\/ Prompt the user and read the year of birth as integer\r\n    printf(\"Please enter your year of birth: \");\r\n    scanf(\"%d\", &amp;year);  \r\n\r\n    int hasCelebratedBirthday;\r\n    printf(\"Enter 1 if you've already had your birthday this year\\n\");\r\n    printf(\"Enter 0 if you have not.\\n\");\r\n    scanf(\"%d\", &amp;hasCelebratedBirthday);\r\n\r\n    \/\/ Calculating age depending on the user's birthday\r\n    \/\/ Note: There is a bug in this code that only triggers on an edge case.\r\n    \/\/       Can you find it?\r\n    int age;\r\n    if (hasCelebratedBirthday)\r\n    {\r\n        age = 2025 - year;\r\n    }\r\n    else\r\n    {\r\n        age = 2025 - year - 1;\r\n    }\r\n    \r\n    \/\/ Printing the result\r\n    printf(\"You are %d years of age\\n\", age);\r\n\r\n    \/\/ The program finished successfully. Integer 0 indicates success.\r\n    return 0; \r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example is the written version of the first lesson with additional commentary that is intended to support you in going over the code that you built during the class. This example contains the following Part 1 of the hello.c code with linear flow Part 2 of the hello.c code with conditional flow Syntax and &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/hello-c-2025\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">hello.c (2025)<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width-wide.php","meta":{"footnotes":""},"class_list":["post-10325","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/10325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/comments?post=10325"}],"version-history":[{"count":10,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/10325\/revisions"}],"predecessor-version":[{"id":10484,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/10325\/revisions\/10484"}],"up":[{"embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/1521"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=10325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}