1

I'm currently in the process of learning to use OpenGL, and to start I'm following a series of tutorials from http://www.opengl-tutorial.org/.

In the first tutorial, they begin main() with this if-statement:

// Initialize GLFW
if (!glfwInit() ) 
{
  fprintf( stderr, "failed to initialize GLFW\n" );
  getchar();
  return -1;
}

I understand that fprintf is an unbuffered print that's kind of the standard for indicating an error. And return -1 makes sense too. But what's the point of calling getchar() after indicating an error?

Thanks.

BDL
  • 21,052
  • 22
  • 49
  • 55

2 Answers2

6

It's probably because the .exe is a command-line utility. Without the getchar() call, if a user simply double-clicks the .exe on Windows, and the program gets an error right away and exits, the user will just see a command prompt window pop up and then disappear, and never see the error. Waiting for user input gives the user a chance to see what happened.

Dan Korn
  • 1,274
  • 9
  • 14
1

getchar() is used to pause the application after the execution in order to leave the command line and the output visible to the user.

Without getchar function the application would exit immediately and, if it was launched from a graphical user interface, the results/errors would not be visible.

Francesco Argese
  • 626
  • 4
  • 11