Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The arguments <code>argc</code> and <code>argv</code> of <code>main</code> is used as a way to send arguments to a program, the possibly most familiar way is to use the good ol' terminal where an user could type <kbd><code>cat file</code></kbd>. Here the word <code>cat</code> is a program that takes a file and outputs it to standard output (<code>stdout</code>).</p> <p>The program receives the number of arguments in <code>argc</code> and the vector of arguments in <code>argv</code>, in the above the <strong>arg</strong>ument <strong>c</strong>ount would be two (The program name counts as the first argument) and the <strong>arg</strong>ument <strong>v</strong>ector would contain [<code>cat</code>,<code>file</code>,<em>null</em>]. While the last element being a null-pointer.</p> <p>Commonly, you would write it like this:</p> <pre><code>int // Specifies that type of variable the function returns. // main() must return an integer main ( int argc, char **argv ) { // code return 0; // Indicates that everything went well. } </code></pre> <p>If your program does not require any arguments, it is equally valid to write a <code>main</code>-function in the following fashion:</p> <pre><code>int main() { // code return 0; // Zero indicates success, while any // Non-Zero value indicates a failure/error } </code></pre> <p>In the early versions of the C language, there was no <code>int</code> before <code>main</code> as this was implied. Today, this is considered to be an error.</p> <p>On <strong>POSIX</strong>-compliant systems (and Windows), there exists the possibility to use a third parameter <code>char **envp</code> which contains a vector of the programs <a href="http://en.wikipedia.org/wiki/Environment_variable" rel="noreferrer"><strong>env</strong>ironment variables</a>. Further variations of the argument list of the <code>main</code> function exists, but I will not detail it here since it is non-standard.</p> <p>Also, the naming of the variables is a <em>convention</em> and has no actual meaning. It is always a good idea to adhere to this so that you do not confuse others, but it would be equally valid to define <code>main</code> as</p> <pre><code>int main(int c, char **v, char **e) { // code return 0; } </code></pre> <p>And for your second question, there are several ways to send arguments to a program. I would recommend you to look at the <a href="http://linux.die.net/man/3/exec" rel="noreferrer"><code>exec*()</code>family of functions</a> which is <strong>POSIX</strong>-standard, but it is probably <strong>easier</strong> to just use <a href="http://linux.die.net/man/3/system" rel="noreferrer"><code>system</code></a><code>("command arg1 arg2")</code>, but the use of <code>system()</code> is usually frowned upon as it is not guaranteed to work on every system. I have not tested it myself; but if there is no <code>bash</code>,<code>zsh</code>, or other shell installed on a *NIX-system, <code>system()</code> will fail.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload