{"id":2115,"date":"2015-11-09T00:10:08","date_gmt":"2015-11-08T22:10:08","guid":{"rendered":"http:\/\/www.blue.pri.ee\/ttu\/?page_id=2115"},"modified":"2024-09-05T19:18:38","modified_gmt":"2024-09-05T17:18:38","slug":"parse-c","status":"publish","type":"page","link":"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/parse-c\/","title":{"rendered":"Accessing command line arguments"},"content":{"rendered":"<pre class=\"lang:c decode:true \">\/**\r\n * File:        args.c\r\n * Author:      Risto Heinsar\r\n * Created:     10.11.2014\r\n * Modified:    12.08.2024\r\n *\r\n * Description: Program demonstrates accessing command line arguments\r\n *              when passed during the execution of the program.\r\n * \r\n * Usage:       .\/program_name arg1 arg2 arg3 ...\r\n *\/\r\n\r\n#include &lt;stdio.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    printf(\"Got %d command line arguments\\n\", argc);\r\n    \r\n    \/\/ Loops over all arguments as strings\r\n    for (int i = 0; i &lt; argc; i++)\r\n    {\r\n        printf(\"argv[%d] = %s\\n\", i, argv[i]);\r\n    }\r\n    printf(\"\\n\");\r\n    \r\n    \/\/ Loop over the arguments once more\r\n    for (int i = 0; i &lt; argc; i++)\r\n    {\r\n        \/\/ Grab the length of the current argument\r\n        int n = strlen(argv[i]);\r\n        \r\n        \/\/ Print the current argument, character by character\r\n        for (int j = 0; j &lt; n; j++)\r\n        {\r\n            printf(\"%c \", argv[i][j]);\r\n        }\r\n        printf(\"\\n\");\r\n    }\r\n    \r\n    return 0;\r\n}\r\n<\/pre>\n<p>Sample execution of a program using command line arguments<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-7008\" src=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/08\/Screenshot_20220815_172826.png\" alt=\"\" width=\"608\" height=\"200\" srcset=\"https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/08\/Screenshot_20220815_172826.png 608w, https:\/\/blue.pri.ee\/ttu\/wp-content\/uploads\/2022\/08\/Screenshot_20220815_172826-300x99.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><\/p>\n<p><strong>Some notable points<\/strong><\/p>\n<ul>\n<li>This form of main function ( <span class=\"lang:default highlight:0 decode:true crayon-inline\" style=\"font-size: 12.8px;\">int main(int argc, char *argv[])<\/span> ) should only be used when you intend to accept command line arguments.<\/li>\n<li>It doesn&#8217;t matter which form of argument vector you are using. Both\u00a0\u00a0 <span class=\"lang:default highlight:0 decode:true crayon-inline\">*argv[]<\/span>\u00a0 and <span class=\"lang:default highlight:0 decode:true crayon-inline\">**argv<\/span>\u00a0 are equivalent.<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">argc<\/span>\u00a0 will tell you how many arguments were passed to your program. This is also the length of the argument vector.<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">argc<\/span>\u00a0 value will be 1, if no arguments were passed to the program.<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">argv<\/span>\u00a0 is the argument vector that contains all passed arguments as strings. It reality it&#8217;s an array of pointers to strings. Each pointer will be pointing at one of the arguments (strings).<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">argv[0]<\/span>\u00a0 contains the name of the executable (and how it was executed)<\/li>\n<li><span class=\"lang:default highlight:0 decode:true crayon-inline\">argv[i]<\/span>\u00a0 contains the i-th argument as a string. I.e. \u00a0<span class=\"lang:default highlight:0 decode:true crayon-inline \">argv[1]<\/span>\u00a0 contains the first argument.<\/li>\n<li>All argumetns are passed as strings, even if they only contain numbers.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\/** * File: args.c * Author: Risto Heinsar * Created: 10.11.2014 * Modified: 12.08.2024 * * Description: Program demonstrates accessing command line arguments * when passed during the execution of the program. * * Usage: .\/program_name arg1 arg2 arg3 &#8230; *\/ #include &lt;stdio.h&gt; #include &lt;string.h&gt; int main(int argc, char *argv[]) { printf(&#8220;Got %d command line &hellip; <a href=\"https:\/\/blue.pri.ee\/ttu\/programming-i\/samples\/parse-c\/\" class=\"more-link\">Loe edasi <span class=\"screen-reader-text\">Accessing command line arguments<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1521,"menu_order":10,"comment_status":"closed","ping_status":"closed","template":"page-templates\/code-width.php","meta":{"footnotes":""},"class_list":["post-2115","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2115","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=2115"}],"version-history":[{"count":3,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2115\/revisions"}],"predecessor-version":[{"id":9525,"href":"https:\/\/blue.pri.ee\/ttu\/wp-json\/wp\/v2\/pages\/2115\/revisions\/9525"}],"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=2115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}