{"id":82799,"date":"2021-07-28T09:00:04","date_gmt":"2021-07-28T03:30:04","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=82799"},"modified":"2021-07-28T09:00:04","modified_gmt":"2021-07-28T03:30:04","slug":"command-line-arguments-in-c","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/","title":{"rendered":"Command Line Arguments in C"},"content":{"rendered":"<p>C language offers various types of functionalities and features to programmers. Programmers find C useful for solving several problems. It is very easy to implement because of its simplicity and easily understandable syntaxes.<\/p>\n<p>But in C, we can also give command-line arguments to the program from the terminal.<\/p>\n<p>Let us learn more about the command Line Arguments in C.<\/p>\n<h3>What are Command Line Arguments in C?<\/h3>\n<p>Command line arguments are the arguments which the user gives from the operating system\u2019s command line during the time of execution. Earlier, we used main() functions without arguments. These command line arguments are handled by the main() function.<\/p>\n<p>If you want to pass command line arguments then you will have to define the main() function with two arguments. The first argument defines the number of command line arguments and the second argument is the list of command line arguments.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">returnType_Function (*pointer_name) ( list_of_arguments)<\/pre>\n<p>In the above syntax, we have to specify the return type of the function such as int, void, double etc. Then, give the name to the function pointer and then list the arguments.<\/p>\n<p><strong>Syntax:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">void ( *pointerToFunction ) ( int );\n<\/pre>\n<p>In the above syntax, <strong>pointerToFunction<\/strong> is the pointer to a function. It takes an integer argument and the return type is void.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">int main(int argc, char *argv[] )\n{\n\/\/ body;\n}<\/pre>\n<ul>\n<li><strong>argc:-<\/strong> It is known as argument count. It is int. It stores the number of the command line arguments passed by a user from the terminal and also stores the name of the program. The value of argc must not be negative.<\/li>\n<li><strong>argv:-<\/strong> It is a pointer array and it points to every argument which is being passed to the program. Argv[0] denotes the name of the program.<\/li>\n<li>Till <strong>argv[argc-1],<\/strong> every argument is a command line argument.<\/li>\n<\/ul>\n<h3>Working of Function Pointer in C:-<\/h3>\n<p>Let\u2019s see how a function pointer works with some examples.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nint cal_sum(int n1, int n2)\n{\nreturn n1+n2;\n}\nint main()\n{\nint(*functionPointer)(int, int) ;\nfunctionPointer = cal_sum ;\n\/\/Calling using the function pointer!\nint res1 = functionPointer(3,69) ;\n\/\/Calling function in a normal way!\nint res2 = cal_sum(5,69) ;\nprintf ( \"Through function pointer: %d\\n\" ,res1) ;\nprintf ( \"Through function name: %d\\n\" ,res2) ;\nreturn 0 ;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Through function pointer: 72<br \/>\nThrough function name: 74<\/div>\n<p>In the above example, we have declared a method named cal_sum which takes two parameters of int data type and adds them. We also declared a function pointer named <strong>functionPointer<\/strong> which takes two inputs from the user.<\/p>\n<p>We got two outputs. One is through calling the function pointer and the second one is through calling the function name.<\/p>\n<p><strong>Example:-<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nvoid subs(int n1,int n2)\n{\nprintf (\"The subtraction between n1 and n2 is %d!\\n\", n1-n2);\n}\nint main()\n{\nvoid (*funcpoint[])(int,int) = {subs} ;\nint n1=4,n2=2;\nprintf(\"TechVidvan Tutorial: Doing subtraction using Function pointer!\\n\");\n(*funcpoint[0])(n1,n2) ;\nreturn 0 ;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Doing subtraction using Function pointer!<br \/>\nThe subtraction between n1 and n2 is 2!<\/div>\n<p>In the above example, we created a function named subs separately above the main function. We also declared a function pointer with two integer types n1 and n2.<\/p>\n<h3>Example:- Command Line Arguments<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;  \nvoid main(int argc, char *argv[] ){\nprintf(\"TechVidvan Tutorial: Example of Command Line Arguments in C!\\n\");\nprintf(\"Program name is: %s\\n\",argv[0]);\nprintf(\"Arguments passes: %d\\n\", argc);  \nif(argc &lt; 2){  \nprintf(\"You did not pass any arguments\\n\");  \n}\nelse{  \nprintf(\"First argument: %s\\n\", argv[1]);  \n}  \n}<\/pre>\n<p><strong>Input:-<\/strong><br \/>\n.\/a.out Hi<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Example of Command Line Arguments in C!<br \/>\nProgram name is: .\/a.out<br \/>\nArguments passes: 2<br \/>\nFirst argument: Hi<\/div>\n<h3>Properties of Command-Line arguments in C:-<\/h3>\n<ul>\n<li><strong>argv[0]<\/strong> prints the name of the program.<\/li>\n<li><strong>argv[1]<\/strong> prints the first argument which is provided by the user.<\/li>\n<li><strong>argv[argc]<\/strong> is a null pointer.<\/li>\n<li>Always passed to the main() function.<\/li>\n<li>Command Line Arguments are passed by the user from the terminal.<\/li>\n<li>It is used to control programs from the outside.<\/li>\n<\/ul>\n<h3>Example:- Command Line Arguments in C<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include&lt;stdio.h&gt;\nint main(int argc,char* argv[])\n{\nint i;\nprintf(\"Program Name is: %s\\n\",argv[0]);\nprintf(\"TechVidvan Tutorial: Using Command Line Arguments in C!\\n\\n\");\nif(argc==1)\nprintf(\"You did not provide any arguments!\\n\");\nif(argc&gt;=2)\n{\nprintf(\"Arguments Passed: %d.\\n\\n\",argc);\nprintf(\"!!!!!!!!Below are the arguments which you passed!!!!!!!!\\n\");\nfor(i=0;i&lt;argc;i++)\nprintf(\"argv[%d]: %s\\n\",i,argv[i]);\n}\nreturn 0;\n}<\/pre>\n<h4>Output in Different Scenarios:-<\/h4>\n<ul>\n<li>Without Argument:- When you did not pass any arguments through the command line then it prints the following output.<\/li>\n<\/ul>\n<p><strong>Input:-<\/strong><br \/>\n.\/a.out<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Program Name is: .\/a.out<br \/>\nTechVidvan Tutorial: Using Command Line Arguments in C!You did not provide any arguments!<\/div>\n<ul>\n<li><strong>Single Argument:-<\/strong> When you pass a single argument by space but inside double quotes then it will produce the following output.<\/li>\n<\/ul>\n<p><strong>Input:-<\/strong><br \/>\n.\/a.out &#8220;TechVidvan Tutorial&#8221;<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Program Name is: .\/a.out<br \/>\nTechVidvan Tutorial: Using Command Line Arguments in C!Arguments Passed: 2.!!!!!!!!Below are the arguments which you passed!!!!!!!!<br \/>\nargv[0]: .\/a.out<br \/>\nargv[1]: TechVidvan Tutorial<\/div>\n<ul>\n<li><strong>Three Arguments:-<\/strong> When you pass three arguments to the program through the terminal then it will produce the following output.<\/li>\n<\/ul>\n<p><strong>Input:-<\/strong><br \/>\n.\/a.out How You doing<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Program Name is: .\/a.out<br \/>\nTechVidvan Tutorial: Using Command Line Arguments in C!Arguments Passed: 4.!!!!!!!!Below are the arguments which you passed!!!!!!!!<br \/>\nargv[0]: .\/a.out<br \/>\nargv[1]: How<br \/>\nargv[2]: You<br \/>\nargv[3]: doing<\/div>\n<p><strong>Single Argument:-<\/strong> When you pass a single argument by space but inside single quotes then it will produce the following output.<br \/>\n<strong>Input:-<\/strong><br \/>\n.\/a.out &#8216;TechVidvan Tutorial&#8217;<\/p>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">Program Name is: .\/a.out<br \/>\nTechVidvan Tutorial: Using Command Line Arguments in C!Arguments Passed: 2.!!!!!!!!Below are the arguments which you passed!!!!!!!!<br \/>\nargv[0]: .\/a.out<br \/>\nargv[1]: TechVidvan Tutorial<\/p>\n<\/div>\n<h4>Usage of Command-Line Arguments in C:-<\/h4>\n<ul>\n<li>It allows the use of standard input and output so that we can chain commands using shell.<\/li>\n<li>It is used to simplify the installation and use of simple programs.<\/li>\n<\/ul>\n<h3>Read Data from user:-<\/h3>\n<p>We use <strong>scanf()<\/strong> function to read data from the user. Following is the program to get data from the user using the <strong>scanf()<\/strong> function.<\/p>\n<p><strong>Example:- scanf() function<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">#include &lt;stdio.h&gt;\nint main() {\nint x;\nscanf(\"%d\", &amp;x);\nprintf(\"TechVidvan Tutorial: Reading data from the user using scanf()!\\n\");\nprintf(\"Value of x is: %d\\n\",x);\nreturn 0;\n}<\/pre>\n<p><strong>Output:-<\/strong><\/p>\n<div class=\"code-output\">TechVidvan Tutorial: Reading data from the user using scanf()!<br \/>\nValue of x is: 20<\/div>\n<h3>Summary<\/h3>\n<p>In this tutorial, we learnt about what command-line arguments are and how to use them in the C programming language. We also discussed the working of function pointers. And we also showed how you can use command-line arguments in your program code.<\/p>\n<p>Command-line arguments are very helpful to any programmer. With command-line arguments, you can give data from the terminal of your operating system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C language offers various types of functionalities and features to programmers. Programmers find C useful for solving several problems. It is very easy to implement because of its simplicity and easily understandable syntaxes. But&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":82922,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3510],"tags":[3862,3863],"class_list":["post-82799","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-command-line-arguments-in-c","tag-properties-of-command-line-arguments-in-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Command Line Arguments in C - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about command-line arguments in C &amp; how to use them. See the working of function pointers. See the properties of command line argument.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-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 - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about command-line arguments in C &amp; how to use them. See the working of function pointers. See the properties of command line argument.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-28T03:30:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Command-Line-Arguments-in-C.jpg\" \/>\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\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\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=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Command Line Arguments in C - TechVidvan","description":"Learn about command-line arguments in C & how to use them. See the working of function pointers. See the properties of command line argument.","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:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Command Line Arguments in C - TechVidvan","og_description":"Learn about command-line arguments in C & how to use them. See the working of function pointers. See the properties of command line argument.","og_url":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2021-07-28T03:30:04+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Command-Line-Arguments-in-C.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Command Line Arguments in C","datePublished":"2021-07-28T03:30:04+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/"},"wordCount":896,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Command-Line-Arguments-in-C.jpg","keywords":["Command Line Arguments in C","Properties of command line arguments in C"],"articleSection":["C Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/","url":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/","name":"Command Line Arguments in C - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Command-Line-Arguments-in-C.jpg","datePublished":"2021-07-28T03:30:04+00:00","description":"Learn about command-line arguments in C & how to use them. See the working of function pointers. See the properties of command line argument.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Command-Line-Arguments-in-C.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2021\/07\/Command-Line-Arguments-in-C.jpg","width":1200,"height":628,"caption":"Command Line Arguments in C"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/command-line-arguments-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Command Line Arguments in C"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82799","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=82799"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/82799\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/82922"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=82799"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=82799"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=82799"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}