title: segmentation fault checking user arguments c
body: <p>I am trying to validate user input from the command line with argc and argv but am running into a segmentation fault and am not sure why.</p>
  
  <p>I want to validate the user has entered a positive int (so argc =2 and argv is positive).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>int main (int argc, string argv[])
  {
      int k = atoi(argv[1]);
      if (argc != 2 &amp;&amp; k &lt;1)
      {
            return 1;
      }
  }
  </code></pre>
  
  <p>The k &lt; 1 part of my code seems to be working fine, but I get segmentation errors every time I try to run without the program name and an argument (agrc !=2).</p>
  
  <p>EDIT:</p>
  
  <p>More clear description of the error handling problem:</p>
  
  <p>If I run ./myprogram -2
  then I get my return 1 as expected. </p>
  
  <p>If I run ./myprogram hjdksfhdsk
  then I get my return 1 as expected. </p>
  
  <p>If I run ./myprogram 
  then I get a segmentation fault. </p>
  
  <p>It is this behaviour that makes me think my handling of the argc value is at fault, but I am not sure why I cannot just test argc !=2.</p>