This repository was archived by the owner on Jul 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand-line-arguments-in-c.html
60 lines (54 loc) · 3.16 KB
/
command-line-arguments-in-c.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><meta http-equiv="X-UA-Compatible" content="ie=edge"><meta name="description" content="Creating terminal applications in C often relies on command line arguments."><!-- Bing --><meta name="msvalidate.01" content="45CBBE1BD8265A2217DFDA630EB8F84A" /><title>Tiny Brain Fans - Command Line Arguments in C</title><link rel="stylesheet" href="tinystyle.css"></head><body>
<main id="main"><article id="content"><h1 id="title">Command Line Arguments in C</h1><p>Creating terminal applications in <a href="c.html">C</a> often relies on command line arguments.</p>
<h2>Arguments</h2>
<p>The main function of a C program takes in two arguments to handle command line arguments:</p>
<pre><code class="language-c">int main (int argc, char *argv[])
{
...
}
</code></pre>
<p><code>argc</code> is the number (<strong>c</strong>ount) of arguments provided, including the executable name. <code>argv</code> is the array of arguments, with item 0 being the name of the executable.</p>
<h2>Example</h2>
<p>This example uses <a href="curses.html">ncurses</a>. It should print out all arguments you provide beyond the executable and wait for user entry before exiting.</p>
<p><strong>showargs.c</strong></p>
<pre><code class="language-c">#include <string.h>
#include <ncurses.h>
int main (int argc, char *argv[])
{
char task[255];
initscr();
if (argc == 1)
{
addstr("No arguments provided.");
getch();
endwin();
return 0;
}
else
{
strcpy(task, argv[1]);
for (int i = 2; i < argc; i++)
{
strcat(task, " ");
strcat(task, argv[i]);
}
printw("%s", task);
getch();
endwin();
return 0;
}
}
</code></pre>
<p><strong>Compilation</strong></p>
<pre><code class="language-bash">$ gcc -lncurses showargs.c -o showargs
</code></pre>
<p><strong>Executing</strong></p>
<pre><code class="language-bash">$ ./showargs You can print all of these out
# Will display "You can print all of these out"
</code></pre>
<h2>References</h2>
<ol>
<li><a href="https://duckduckgo.com/?t=ffab&q=argc+argv+c&ia=web" target="_blank">https://duckduckgo.com/?t=ffab&q=argc+argv+c&ia=web</a></li>
<li><a href="https://stackoverflow.com/questions/7174216/how-can-i-concatenate-arguments-in-to-a-string-in-c" target="_blank">https://stackoverflow.com/questions/7174216/how-can-i-concatenate-arguments-in-to-a-string-in-c</a></li>
</ol>
<p class="last-modified">Last modified: 202206101419</p></article></main><footer><nav><a href="index.html">Sitemap</a></nav><div class="social"><p>Built using <a href="http://codeberg.org/milofultz/swiki" target="_blank" rel="noopener noreferrer">{{SWIKI}}</a></p><p><a href="http://codeberg.org/milofultz/" target="_blank" rel="noopener noreferrer">Codeberg</a></p><p><a href="http://milofultz.com/" target="_blank" rel="noopener noreferrer">milofultz.com</a></p><p><a href="https://merveilles.town/@milofultz" target="_blank" rel="me noopener noreferrer">Mastodon</a></p></div></footer></body></html>