{"id":5583,"date":"2020-11-15T22:36:49","date_gmt":"2020-11-15T20:36:49","guid":{"rendered":"https:\/\/blue.pri.ee\/ttu\/?p=5583"},"modified":"2025-11-30T20:06:06","modified_gmt":"2025-11-30T18:06:06","slug":"14-lab-command-line-arguments","status":"publish","type":"post","link":"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/","title":{"rendered":"PR1EN14: Command line arguments"},"content":{"rendered":"<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\/labs\/pr1_en\/14-lab-command-line-arguments\/#Lab_materials\" >Lab materials<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Lab_tasks\" >Lab tasks<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Lab_task_W14-1_Calculator\" >Lab task [W14-1]: Calculator<\/a><ul class='ez-toc-list-level-5' ><li class='ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Requirements\" >Requirements<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Recommended_list_of_functions\" >Recommended list of functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Example\" >Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-5'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Test_cases\" >Test cases<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Extra_task_1_W14-2_Real_numbers\" >Extra task 1 [W14-2]: Real numbers<\/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\/labs\/pr1_en\/14-lab-command-line-arguments\/#Extra_task_2_W14-3_Extended_functionality\" >Extra task 2 [W14-3]: Extended functionality<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#After_the_class_you_should_be_able_to\" >After the class, you should be able to<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/#Additional_content\" >Additional content<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Lab_materials\"><\/span><strong>Lab materials<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Slides: <a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583\/slaidid-en\/14_command_line_arguments.pdf\"><strong>Command line arguments<\/strong><\/a><\/li>\n<li>Sample calculator program [<strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583_en\/calculator.exe\">windows<\/a><\/strong>] [<strong><a href=\"https:\/\/blue.pri.ee\/ttu\/files\/iax0583_en\/calculator\">linux<\/a><\/strong>]<\/li>\n<li data-select-like-a-boss=\"1\">Code sample: <strong><a style=\"outline-width: 0px !important; user-select: auto !important;\" href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/parse-c\/\">Accepting arguments at runtime<\/a><\/strong><\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Lab_tasks\"><\/span>Lab tasks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>For this lab, you will have one main task with two extra tasks that add extra functionality and features to the base task.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Lab_task_W14-1_Calculator\"><\/span>Lab task [W14-1]: Calculator<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>For this task, you will build a calculator that works by getting its input from the command line<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Requirements\"><\/span>Requirements<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>You will create a simple calculator, that\n<ul>\n<li>can do addition, subtraction, multiplication and division<\/li>\n<li>supports positive integers as operands<\/li>\n<li>only does one operation at a time (e.g. 3 + 6)<\/li>\n<\/ul>\n<\/li>\n<li>Have a precision of 2 places after the comma<\/li>\n<li>Both the operands and the operator will be given as command line arguments\n<ul>\n<li>E.g. an addition operation would look like this:<\/li>\n<\/ul>\n<\/li>\n<li>Program is not allowed to ask for any input during its execution<\/li>\n<li>Your calculator must detect the following errors\n<ul>\n<li>Wrong argument count<\/li>\n<li>Unknown operator<\/li>\n<li>Non-numeric operand<\/li>\n<li>Division by zero<\/li>\n<\/ul>\n<\/li>\n<li>When an error occurs, you will both present the specific error message and the guide how to use your program<\/li>\n<\/ul>\n<h5><span class=\"ez-toc-section\" id=\"Recommended_list_of_functions\"><\/span>Recommended list of functions<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<ul>\n<li>Display help\n<ul>\n<li>Shows how to use the program<\/li>\n<\/ul>\n<\/li>\n<li>Argument check\n<ul>\n<li>Checks for number of operands<\/li>\n<li>For the extra task, checks for <span class=\"lang:default highlight:0 decode:true crayon-inline \">&#8211;help<\/span>\u00a0 argument<\/li>\n<\/ul>\n<\/li>\n<li>Error management\n<ul>\n<li>Displays the specific error message<\/li>\n<li>Calls the display help function<\/li>\n<\/ul>\n<\/li>\n<li>Operand check\n<ul>\n<li>Checks if the operand is numeric<\/li>\n<li>Returns the operand converted to a number (e.g. int)<\/li>\n<\/ul>\n<\/li>\n<li>Calculation\n<ul>\n<li>Identifies the operation and calculates the answer<\/li>\n<li>Returns the answer<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>You may also benefit from creating a function to check for the operator.<\/p>\n<h5><span class=\"ez-toc-section\" id=\"Example\"><\/span>Example<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<p>To test a completed program, we recommend downloading the sample and seeing how it works.<\/p>\n<p>To see all the test cases for the program, check the next paragraph.<\/p>\n<p><strong>Example 1: <\/strong>Input is OK and the program prints the answer<\/p>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true\">risto@risto-lt3-tux:~$ .\/calculator 1 \/ 4\r\n&gt; 0.25<\/pre>\n<p><strong>Example 2:<\/strong> Program is executed without arguments. An error message and a how to use guide is provided.<\/p>\n<pre class=\"theme:cisco-router toolbar:1 nums:false lang:default highlight:0 decode:true \">risto@risto-lt3-tux:~\/Desktop$ .\/calculator\r\nError 0! Invalid argument count or unknown argument(s)\r\n\r\nUsage: .\/calculator operand operation operand\r\n\r\nPossible operations:\r\n+ add\r\n- subtract\r\n* multiply\r\n\/ divide\r\n\r\nNote, that due to how shell works,\r\nthe multiplication operator has to be encased in quotes\r\ne.g. 2 \"*\" 3.5<\/pre>\n<h5><span class=\"ez-toc-section\" id=\"Test_cases\"><\/span>Test cases<span class=\"ez-toc-section-end\"><\/span><\/h5>\n<pre class=\"toolbar:1 nums:false lang:ps highlight:0 decode:true\" title=\"testcases.sh\"># base case, normal operations\r\n.\/calculator 1 + 1\r\n.\/calculator 7 \/ 5\r\n\r\n# base case, incorrect input\r\n.\/calculator # wrong argument count\r\n.\/calculator 1 2 3 4 5 # wrong argument count\r\n.\/calculator 6 + a # non-numeric operand\r\n.\/calculator 6 +- 5 # unknown operation\r\n.\/calculator 6 + 6a # non-numeric operand\r\n.\/calculator 6 n 6 # unknown operation\r\n.\/calculator 6 \/ 0 # division by zero\r\n\r\n# Extra task, normal operation\r\n.\/calculator 3.0 \/ 5.0 \r\n.\/calculator -3.0 \/ 5.0 \r\n\r\n# Extra task, incorrect input\r\n.\/calculator 5.5.5 \/ 5 \r\n.\/calculator -5-5 \/ 5 \r\n.\/calculator 5-5 \/ 5<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"Extra_task_1_W14-2_Real_numbers\"><\/span>Extra task 1 [W14-2]: Real numbers<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Change your program in a way that it would support all real numbers. You must add support for both negative numbers and numbers with decimal places.<\/p>\n<p>Test the following<\/p>\n<ul>\n<li>-3.3<\/li>\n<li>3-3<\/li>\n<li>-3-3<\/li>\n<li>3.3.3<\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Extra_task_2_W14-3_Extended_functionality\"><\/span>Extra task 2 [W14-3]: Extended functionality<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Add the following functionality to your program<\/p>\n<ul>\n<li>Taking a square root<\/li>\n<li>Power function (x<sup>y<\/sup>)<\/li>\n<li>Displaying help using the argument <span class=\"lang:default highlight:0 decode:true crayon-inline \">&#8211;help<\/span>\u00a0 without causing an error to occur.<\/li>\n<\/ul>\n<p>All added functions must be present when help is shown.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"After_the_class_you_should_be_able_to\"><\/span><span id=\"After_the_class,_you_should_be_able_to\" class=\"ez-toc-section\"><\/span><strong>After the class, you should be able to<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li>Understand what a command line argument is<\/li>\n<li>Know other programs that use command line arguments<\/li>\n<li>Be able to accept command line arguments in C code<\/li>\n<li>Know that main() function has multiple forms\n<ul>\n<li>Understand what argument count is<\/li>\n<li>Understand what argument vector is<\/li>\n<\/ul>\n<\/li>\n<li>Know what is the first argument passed to a program<\/li>\n<li>Know how argument count changes depending on how many arguments are passed<\/li>\n<li>Understand that * and [] interchangeable<\/li>\n<li>Understand what * means in the command prompt and how to pass such characters to programs<\/li>\n<li>Understand how to pass multiple words as a single argument<\/li>\n<li>Be able to convert strings into integers, floats<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"Additional_content\"><\/span><strong>Additional content<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/command-line-arguments-in-c-cpp\/\">https:\/\/www.geeksforgeeks.org\/command-line-arguments-in-c-cpp\/<\/a><\/li>\n<li><a href=\"https:\/\/www.geeksforgeeks.org\/getopt-function-in-c-to-parse-command-line-arguments\/?ref=rp\">https:\/\/www.geeksforgeeks.org\/getopt-function-in-c-to-parse-command-line-arguments\/?ref=rp<\/a><\/li>\n<li>CS50 (command line arguments): <a href=\"https:\/\/www.youtube.com\/watch?v=AI6Ccfno6Pk\">https:\/\/www.youtube.com\/watch?v=AI6Ccfno6Pk<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Lab materials Slides: Command line arguments Sample calculator program [windows] [linux] Code sample: Accepting arguments at runtime Lab tasks For this lab, you will have one main task with two extra tasks that add extra functionality and features to the base task. Lab task [W14-1]: Calculator For this task, you will build a calculator that &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/labs\/pr1_en\/14-lab-command-line-arguments\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">PR1EN14: Command line arguments<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[],"class_list":["post-5583","post","type-post","status-publish","format-standard","hentry","category-pr1_en"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/types\/post"}],"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=5583"}],"version-history":[{"count":6,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5583\/revisions"}],"predecessor-version":[{"id":10947,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/posts\/5583\/revisions\/10947"}],"wp:attachment":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/media?parent=5583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/categories?post=5583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/tags?post=5583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}