<p>I'm trying to solve <a href="http://uva.onlinejudge.org/external/1/100.pdf" rel="noreferrer">the 3n+1 problem</a> and I have a <code>for</code> loop that looks like this: </p>

<pre><code>for(int i = low; i &lt;= high; ++i)
        {
                res = runalg(i);
                if (res &gt; highestres)
                {
                        highestres = res;
                }

        }
</code></pre>

<p>Unfortunately I'm getting this error when I try to compile with GCC:</p>

<blockquote>
  <p>3np1.c:15: error: 'for' loop initial
  declaration used outside C99 mode</p>
</blockquote>

<p>I don't know what C99 mode is. Any ideas?</p>

[{'answer_id': 24882, 'author': 'OysterD', 'author_id': 2638, 'author_profile': 'https://Stackoverflow.com/users/2638', 'pm_score': 8, 'selected': True, 'text': '<p>I\'d try to declare <code>i</code> outside of the loop!</p>\n\n<p>Good luck on solving 3n+1 :-)</p>\n\n<p>Here\'s an example: </p>\n\n<pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;\n\nint main() {\n\n   int i;\n\n   /* for loop execution */\n   for (i = 10; i &lt; 20; i++) {\n       printf("i: %d\\n", i);\n   }   \n\n   return 0;\n}\n</code></pre>\n\n<p>Read more on for loops in C <a href="https://www.tutorialspoint.com/cprogramming/c_for_loop.htm" rel="noreferrer">here</a>.</p>\n'}, {'answer_id': 24884, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 7, 'selected': False, 'text': '<p>There is a compiler switch which enables <a href="http://en.wikipedia.org/wiki/C99" rel="noreferrer">C99 mode</a>, which amongst other things allows declaration of a variable inside the for loop. To turn it on use the compiler switch <code>-std=c99</code></p>\n\n<p>Or as @OysterD says, declare the variable outside the loop.</p>\n'}, {'answer_id': 24888, 'author': 'Blorgbeard', 'author_id': 369, 'author_profile': 'https://Stackoverflow.com/users/369', 'pm_score': 4, 'selected': False, 'text': "<p>I've gotten this error too.</p>\n\n<pre><code>for (int i=0;i&lt;10;i++) { ..\n</code></pre>\n\n<p>is not valid in the C89/C90 standard. As OysterD says, you need to do:</p>\n\n<pre><code>int i;\nfor (i=0;i&lt;10;i++) { ..\n</code></pre>\n\n<p>Your original code is allowed in C99 and later standards of the C language.</p>\n"}, {'answer_id': 25892, 'author': 'Imran', 'author_id': 1897, 'author_profile': 'https://Stackoverflow.com/users/1897', 'pm_score': 4, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/questions/24881/c-gcc-errors#24888">@Blorgbeard</a>:</p>\n\n<p><strong>New Features in C99</strong></p>\n\n<ul>\n<li>inline functions</li>\n<li>variable declaration no longer restricted to file scope or the start of a compound statement</li>\n<li>several new data types, including long long int, optional extended integer types, an explicit boolean data type, and a complex type to represent complex numbers</li>\n<li>variable-length arrays</li>\n<li>support for one-line comments beginning with //, as in BCPL or C++</li>\n<li>new library functions, such as snprintf</li>\n<li>new header files, such as stdbool.h and inttypes.h</li>\n<li>type-generic math functions (tgmath.h)</li>\n<li>improved support for IEEE floating point</li>\n<li>designated initializers</li>\n<li>compound literals</li>\n<li>support for variadic macros (macros of variable arity)</li>\n<li>restrict qualification to allow more aggressive code optimization</li>\n</ul>\n\n<p><a href="http://en.wikipedia.org/wiki/C99" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/C99</a></p>\n\n<p><a href="http://www.informit.com/guides/content.aspx?g=cplusplus&amp;seqNum=215" rel="nofollow noreferrer">A Tour of C99</a></p>\n'}, {'answer_id': 15871843, 'author': 'Roberto Cuadros', 'author_id': 2230871, 'author_profile': 'https://Stackoverflow.com/users/2230871', 'pm_score': 3, 'selected': False, 'text': '<p>if you compile in C change</p>\n\n<pre><code>for (int i=0;i&lt;10;i++) { ..\n</code></pre>\n\n<p>to</p>\n\n<pre><code>int i;\nfor (i=0;i&lt;10;i++) { ..\n</code></pre>\n\n<p>You can also compile with the C99 switch set. Put -std=c99 in the compilation line:</p>\n\n<pre><code>gcc -std=c99 foo.c -o foo\n</code></pre>\n\n<p>REF: <a href="http://cplusplus.syntaxerrors.info/index.php?title=" rel="noreferrer">http://cplusplus.syntaxerrors.info/index.php?title=</a>\'for\'_loop_initial_declaration_used_outside_C99_mode</p>\n'}, {'answer_id': 16816595, 'author': 'demiurg_spb', 'author_id': 2119881, 'author_profile': 'https://Stackoverflow.com/users/2119881', 'pm_score': 0, 'selected': False, 'text': '<p>For Qt-creator: just add next lines to *.pro file...</p>\n\n<pre><code>QMAKE_CFLAGS_DEBUG = \\\n    -std=gnu99\n\nQMAKE_CFLAGS_RELEASE = \\\n    -std=gnu99\n</code></pre>\n'}, {'answer_id': 18775636, 'author': 'Jihene Stambouli', 'author_id': 2774528, 'author_profile': 'https://Stackoverflow.com/users/2774528', 'pm_score': 1, 'selected': False, 'text': '<p>I had the same problem and it works you just have to declare the <code>i</code> outside of the loop:  </p>\n\n<pre><code>int i;\n\nfor(i = low; i &lt;= high; ++i)\n\n{\n        res = runalg(i);\n        if (res &gt; highestres)\n        {\n                highestres = res;\n        }\n\n}\n</code></pre>\n'}, {'answer_id': 27225785, 'author': 'akelec', 'author_id': 2857477, 'author_profile': 'https://Stackoverflow.com/users/2857477', 'pm_score': 5, 'selected': False, 'text': '<p>To switch to <b>C99</b> mode in <b>CodeBlocks</b>, follow the next steps:</p>\n\n<p>Click <b>Project/Build options</b>, then in tab <b>Compiler Settings</b> choose subtab <b>Other options</b>, and place <code>-std=c99</code> in the text area, and click <b>Ok</b>.</p>\n\n<p>This will turn <b>C99</b> mode on for your Compiler.</p>\n\n<p>I hope this will help someone!</p>\n'}, {'answer_id': 51066676, 'author': 'Mink', 'author_id': 6557223, 'author_profile': 'https://Stackoverflow.com/users/6557223', 'pm_score': 2, 'selected': False, 'text': '<p>For anyone attempting to compile code from an external source that uses an automated build utility such as <em>Make</em>, to avoid having to track down the explicit gcc compilation calls you can set an environment variable. Enter on command prompt or put in .bashrc (or .bash_profile on Mac):</p>\n\n<pre><code>export CFLAGS="-std=c99"\n</code></pre>\n\n<p>Note that a similar solution applies if you run into a similar scenario with C++ compilation that requires C++ 11, you can use:</p>\n\n<pre><code>export CXXFLAGS="-std=c++11"\n</code></pre>\n'}, {'answer_id': 52686151, 'author': 'Dark Burrow', 'author_id': 847192, 'author_profile': 'https://Stackoverflow.com/users/847192', 'pm_score': 2, 'selected': False, 'text': "<p>Jihene Stambouli answered OP question most directly...\nQuestion was;\nwhy does</p>\n\n<pre><code>for(int i = low; i &lt;= high; ++i)\n{\n    res = runalg(i);\n    if (res &gt; highestres)\n    {\n        highestres = res;\n    }\n}\n</code></pre>\n\n<p>produce the error;</p>\n\n<pre><code>3np1.c:15: error: 'for' loop initial declaration used outside C99 mode\n</code></pre>\n\n<p>for which the answer is </p>\n\n<pre><code>for(int i = low...\n</code></pre>\n\n<p>should be</p>\n\n<pre><code>int i;\nfor (i=low...\n</code></pre>\n"}, {'answer_id': 57252368, 'author': 'Jobayer Ahmmed', 'author_id': 5184146, 'author_profile': 'https://Stackoverflow.com/users/5184146', 'pm_score': 2, 'selected': False, 'text': "<p><strong>Enable C99 mode in Code::Blocks 16.01</strong></p>\n\n<ul>\n<li>Go to <strong><em>Settings</em></strong>-> <strong><em>Compiler...</em></strong></li>\n<li>In <strong><em>Compiler Flags</em></strong> section of <strong><em>Compiler settings</em></strong> tab, select checkbox '<strong><em>Have gcc follow the 1999 ISO C language standard [-std=c99]</em></strong>'</li>\n</ul>\n"}]
['https://Stackoverflow.com/questions/24881', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2128/']