title: Range checking for int32_t data type in C++
body: <p>I'm trying to range check a user supplied command line parameter in a C++ program and I'm not sure how to do it.</p>
  <p>If I enter a number that's higher, it gets converted to a lower number.</p>
  <p>What's the preferred method to do the below? Is there a constant that I can reference? And if so, is there a better way to do this range checking?</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdlib.h&gt;
  
  using namespace std;
  
  int main(int argc, char** argv)
  {
      if (argc &lt; 2) {
          cout &lt;&lt; endl &lt;&lt; &quot;usage: &quot; &lt;&lt; argv[0] &lt;&lt; &quot; &lt;port number&gt;&quot; &lt;&lt; endl &lt;&lt; endl;
          return 1;
      }
  
      int32_t port = atoi(argv[1]);
      
      if (port &lt; 1 || port &gt; 65535) {
          cout &lt;&lt; endl &lt;&lt; &quot;port number should be between 1 and 65535&quot; &lt;&lt; endl &lt;&lt; endl;
  
          return 1;
      }
  
      cout &lt;&lt; &quot;port: &quot; &lt;&lt; port &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>