title: How to test if gcc has failed to compile a program in Windows Batch File (cmd)?
body: <p>I have made this random C code (<strong><code>app.c</code></strong>)</p>
  
  <pre><code>int main()
  {
      ERROR; // Just a random code to make sure the compiler fails.
  }
  </code></pre>
  
  <p>and this batch file (<strong><code>run.bat</code></strong>)</p>
  
  <pre><code>@echo off
  :start
  cls
  echo Compiling...
  gcc app.c -o app.exe
  app.exe
  pause
  goto start
  </code></pre>
  
  <p>When I double-click <strong><code>run.bat</code></strong>, it gives me the following output:</p>
  
  <pre><code>Compiling...
  app.c: In function 'main':
  app.c:3:2: error: 'ERROR' undeclared (first use in this function)
    ERROR; // Just a random code to make sure the compiler fails.
    ^~~~~
  app.c:3:2: note: each undeclared identifier is reported only once for each function it appears in
  'app.exe' is not recognized as an internal or external command,
  operable program or batch file.
  Press any key to continue . . .
  </code></pre>
  
  <p>You can notice the last error:</p>
  
  <pre><code>'app.exe' is not recognized as an internal or external command,
      operable program or batch file.
  </code></pre>
  
  <p>That's because there's no <strong><code>app.exe</code></strong> since the compiler has failed to compile it. <strong>To prevent</strong> that last error from happening, I wanted to check if gcc has <strong>succeeded</strong> and <em>if so</em>, <strong>run the app</strong>.</p>
  
  <p>I searched in SO about checking the return values of a program in Batch and then I learned a thing called <code>errorLevel</code> so I tried to use it.</p>
  
  <p>and this is the new <code>run.bat</code> file:</p>
  
  <pre><code>@echo off
  :start
  cls
  echo Compiling...
  gcc app.c -o app.exe
  if %errorlevel% == 0
  (
      cls
      app.exe
  )
  pause
  goto start
  </code></pre>
  
  <p>It <em>immediately exits</em> the app after printing '<code>Compiling...</code>', I guess I do it wrong maybe..</p>
  
  <p>What's the right way to test if GCC has failed to compile a program in Windows?</p>