

{"id":123503,"date":"2024-02-23T18:00:40","date_gmt":"2024-02-23T12:30:40","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=123503"},"modified":"2024-02-23T18:24:57","modified_gmt":"2024-02-23T12:54:57","slug":"command-line-argument-in-c","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/","title":{"rendered":"Command Line Arguments in C"},"content":{"rendered":"<p>The main() function is the entry point for all C programs. It is responsible for initializing the program and executing code. One important feature of main() is that it can accept command-line arguments &#8211; parameters that are passed to the program when it is invoked from the terminal. This allows the program behavior to be dynamic based on input from the user.<\/p>\n<p>Command-line arguments allow you to control C programs from the terminal. You can pass data, options or commands into your program when you run it. This provides flexibility compared to hard-coding values inside the source code. In this guide, we will explore the syntax and usage of command-line arguments in C.<\/p>\n<h2>Definition of Command Line Arguments in C<\/h2>\n<p>Command line arguments consult with the parameters provided by the person when executing an application through the working machine&#8217;s command line interface. In the past, we generally used the principle() characteristic with no parameters.<\/p>\n<p>However, when working with command line arguments, you need to outline the primary() feature with two parameters. The first parameter suggests the count of command line arguments, and at the same time, the second parameter represents the real listing of command line arguments surpassed through the consumer.<\/p>\n<h3>Syntax for Command-Line Arguments in C<\/h3>\n<p><strong>The main() function can be defined to accept command-line arguments in the following syntax:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int main(int argc, char *argv[])<\/pre>\n<p><strong>Here, argc and argv are special parameters provided to main():<\/strong><\/p>\n<ul>\n<li><strong>argc &#8211;<\/strong> This is an integer value representing the number of arguments passed to the program from the command line.<\/li>\n<li><strong>argv &#8211;<\/strong> This is an array of character pointers listing all the arguments.<\/li>\n<\/ul>\n<p>An alternative syntax for argv is to use a double pointer char **argv.<\/p>\n<h3>Understanding argc and argv<\/h3>\n<p><strong>Let&#8217;s look closer at the argc and argv parameters:<\/strong><\/p>\n<ul>\n<li>argc stands for &#8220;argument count&#8221;. It contains the number of arguments passed to the program from the command line. This will be a non-negative integer value. For example, if the user invoked the program with 3 arguments, argc would be 3.<\/li>\n<li>argv stands for &#8220;argument vector&#8221;. It is an array of character pointers that point to the actual arguments.<\/li>\n<\/ul>\n<p>The elements in the argv array range from argv[0] to argv[argc-1]. Each detail is a C-style string representing a controversy.<\/p>\n<p>For example, argv[0] includes the primary argument, that is, the program calls itself. Argv[1] will point to the second argument, and so forth.<\/p>\n<h3>Example: Using Command-Line Arguments in C<\/h3>\n<h4>Example 1:<\/h4>\n<p><strong>Here is a simple C software that demonstrates the usage of command-line arguments:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n  printf(\"Program name %s\\n\", argv[0]);\r\n\r\n  if(argc &gt; 1) {\r\n    printf(\"There are %d arguments:\\n\", argc);\r\n\r\n    \/\/ Print all arguments\r\n    for(int i=0; i &lt; argc; i++) {\r\n      printf(\"Argument %d: %s\\n\", i, argv[i]);\r\n    }\r\n  }\r\n\r\n  else {\r\n    printf(\"No arguments were passed.\\n\");\r\n  }\r\n\r\n  return 0;\r\n}<\/pre>\n<p><strong>Let&#8217;s give an explanation of what this software does:<\/strong><\/p>\n<ul>\n<li>It prints the program call from argv[0].<\/li>\n<li>It tests if argc is greater than 1, which means at least one argument turned into passed.<\/li>\n<li>In that case, it prints the overall quantity of arguments in argc.<\/li>\n<li>Then, it loops through each detail of argv and prints the argument index and price.<\/li>\n<\/ul>\n<p><strong>Now, let&#8217;s compile and run this program with some sample arguments:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$ gcc args.c -o args\r\n$ .\/args apple 10 20 30\r\nProgram name .\/args\r\nThere are 4 arguments:  \r\nArgument 0: .\/args\r\nArgument 1: apple\r\nArgument 2: 10\r\nArgument 3: 20 \r\nArgument 4: 30<\/pre>\n<p>As you can see, the arguments we passed on the command line were available in argv for the program to use. The argc parameter contained the total number of arguments.<\/p>\n<h4>Example 2:<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\r\n\r\nint main(int argc, char *argv[]) {\r\n\r\n  printf(\"\\nProgram name: %s\\n\", argv[0]);\r\n\r\n  if(argc &lt; 2) {\r\n    printf(\"\\nNo command line arguments passed!\\n\\n\");\r\n  }\r\n  else {\r\n    printf(\"\\nCommand line arguments:\\n\"); \r\n\r\n    for(int i=1; i &lt; argc; i++) {\r\n      printf(\"Argument %d: %s\\n\", i, argv[i]);  \r\n    }\r\n    \r\n    printf(\"\\n\");\r\n  }\r\n\r\n  return 0; \r\n}<\/pre>\n<p><strong>Sample Output:<\/strong><\/p>\n<p><strong>Without arguments:<\/strong><br \/>\nProgram name: .\/program<br \/>\nNo command line arguments passed!<\/p>\n<p><strong>With arguments:<\/strong><br \/>\nProgram name: .\/program<br \/>\nCommand line arguments:<br \/>\nArgument 1: arg1<br \/>\nArgument 2: arg2<br \/>\nArgument 3: arg3<\/p>\n<h3>The key points are:<\/h3>\n<ul>\n<li>Print program name from argv[0]<\/li>\n<li>Check argc for arguments<\/li>\n<li>Loop through argv to print each argument<\/li>\n<li>Format output neatly with newlines and spaces<br \/>\nThis demonstrates a clean way to access command-line arguments in C suitable for an article. The code is properly indented and formatted, and the sample output is boxed to showcase the result.<\/li>\n<\/ul>\n<h3>\u00a0Properties of Command Line Arguments in C<\/h3>\n<p><strong>Here are some key properties and characteristics of command-line arguments in C:<\/strong><\/p>\n<ul>\n<li>When the programme starts, arguments are sent to the main() function.<\/li>\n<li>The values come from parameters supplied when running the program.<\/li>\n<li>This allows you to control program behavior from the outside instead of hard-coding values.<\/li>\n<li>argv[argc] will always be a NULL pointer to indicate the end.<\/li>\n<\/ul>\n<h4>Some other notes:<\/h4>\n<ul>\n<li>argv[0] always points to the name of the program itself.<\/li>\n<li>argv[1] points to the first &#8220;real&#8221; argument supplied.<\/li>\n<li>argv[argc-1] points to the last argument.<br \/>\nIf arguments have spaces, they must be enclosed in quotes when passed.<\/li>\n<\/ul>\n<h3>\u00a0Uses of Command Line Argument in C<\/h3>\n<ul>\n<li>Command-line arguments are passed to main() function as argc and argv[]<\/li>\n<li>argc indicates the number of arguments, argv is an array of argument strings<\/li>\n<li>argv[0] is program name, argv[1] first argument, argv[2] second, etc<\/li>\n<li>Arguments can be retrieved as strings and converted to ints or doubles<\/li>\n<li>Useful for automating tasks by passing input data, options, and configs to programs<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>Command-line arguments provide a useful way to make C programs flexible. Instead of hard-coding input, you can accept input dynamically when the program runs. The argc and argv parameters give you access to these arguments in main(). While command-line args may seem complex at first, with some practice you&#8217;ll find them very useful for writing robust programs in C. I encourage you to experiment with them on your own programs!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The main() function is the entry point for all C programs. It is responsible for initializing the program and executing code. One important feature of main() is that it can accept command-line arguments &#8211;&#46;&#46;&#46;<\/p>\n","protected":false},"author":86671,"featured_media":131845,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19488],"tags":[23951,23914,29133,29979,20099,19484,29980],"class_list":["post-123503","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-c-command-line-arguments","tag-c-programming","tag-c-tutorials","tag-command-line-argument-in-c","tag-command-line-arguments","tag-learn-c","tag-what-is-command-line-arguments-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Command Line Arguments in C - DataFlair<\/title>\n<meta name=\"description\" content=\"Command-line arguments allow you to control C programs from the terminal. In this we will explore the syntax and usage of command-line arguments in C.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Command Line Arguments in C - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Command-line arguments allow you to control C programs from the terminal. In this we will explore the syntax and usage of command-line arguments in C.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-02-23T12:30:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-02-23T12:54:57+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/command-line-argument-in-c.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Command Line Arguments in C - DataFlair","description":"Command-line arguments allow you to control C programs from the terminal. In this we will explore the syntax and usage of command-line arguments in C.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Command Line Arguments in C - DataFlair","og_description":"Command-line arguments allow you to control C programs from the terminal. In this we will explore the syntax and usage of command-line arguments in C.","og_url":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2024-02-23T12:30:40+00:00","article_modified_time":"2024-02-23T12:54:57+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/command-line-argument-in-c.webp","type":"image\/webp"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49"},"headline":"Command Line Arguments in C","datePublished":"2024-02-23T12:30:40+00:00","dateModified":"2024-02-23T12:54:57+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/"},"wordCount":850,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/command-line-argument-in-c.webp","keywords":["C Command Line Arguments","c programming","c tutorials","command line argument in c","Command Line Arguments","Learn C","what is command line arguments in c"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/","url":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/","name":"Command Line Arguments in C - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/command-line-argument-in-c.webp","datePublished":"2024-02-23T12:30:40+00:00","dateModified":"2024-02-23T12:54:57+00:00","description":"Command-line arguments allow you to control C programs from the terminal. In this we will explore the syntax and usage of command-line arguments in C.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/command-line-argument-in-c.webp","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2023\/11\/command-line-argument-in-c.webp","width":1200,"height":628,"caption":"command line argument in c"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/command-line-argument-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"C Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/c-programming\/"},{"@type":"ListItem","position":3,"name":"Command Line Arguments in C"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/0e594f928e31fc96628ac40f6ae74f49","name":"TechVidvan Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c89190da3d4010c71ba476b618ab10fdc2335c82cdfa0ad5002d98d0f2473444?s=96&d=mm&r=g","caption":"TechVidvan Team"},"description":"TechVidvan Team provides high-quality content &amp; courses on AI, ML, Data Science, Data Engineering, Data Analytics, programming, Python, DSA, Android, Flutter, full stack web dev, MERN, and many latest technology.","url":"https:\/\/data-flair.training\/blogs\/author\/test001\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123503","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/86671"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=123503"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123503\/revisions"}],"predecessor-version":[{"id":131844,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/123503\/revisions\/131844"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/131845"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=123503"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=123503"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=123503"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}