Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If the user input is always numbers separeted by spaces and then at the end is an enter (newline). Then you can use the following code</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main(int argc, char *argv[]) { int input; char c; while (scanf(" %d%c", &amp;input, &amp;c) == 2 ) { printf("number is %d\n", input); if ( c == '\n') break; } } </code></pre> <hr> <p>If the use want to communicate the number of input as argument</p> <pre><code>int main(int argc, char *argv[]) { int number_of_input = atoi(argv[1]); int input, i; for (i=0; i&lt;number_of_input; i++) { scanf(" %d", &amp;input); } } </code></pre> <p>and when you call you program. you call it in this way:</p> <pre><code>$ myprogram 5 </code></pre> <p>and 5 here is the number of the integer that you can input</p> <ul> <li><code>myprogram</code> will be saved in <code>argv[0]</code></li> <li><code>5</code> will be saved in <code>argv[1]</code></li> </ul> <p><code>myprogram</code> and <code>5</code> are saved as sting in the <code>argv[]</code> array. <code>atoi(argv[1])</code> will convert the <code>"5"</code> as string to <code>5</code> as integer</p> <hr> <p>you can make the user enter an infinite integer input in this way too:</p> <pre><code>int main(int argc, char *argv[]) { int input, i; while (1) { scanf(" %d", &amp;input); } } </code></pre> <p>And you can give the user a way to stop this infinite loop:</p> <pre><code>int main(int argc, char *argv[]) { int input; while (scanf(" %d", &amp;input) != EOF) { //.... } } </code></pre> <p>here you can stop the infinite loop with</p> <p>EOF = <kbd>CTRL</kbd> + <kbd>D</kbd> (for Linux)</p> <p>EOF = <kbd>CTRL</kbd> + <kbd>Z</kbd> (for Windows)</p>
    singulars
    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. 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