How to have a "hit any key" pause

Started by jwzumwalt, April 26, 2009, 02:58:31 AM

Previous topic - Next topic

jwzumwalt

/*
   --------------------------------------------------------------------
   PROGRAM:     hit-any-key.c       Ver: 1.0        Rev: 05/01/2009
   DESCRIPTION: C tutor
   BY:          Jan Zumwalt - www.neatinfo.com
   --------------------------------------------------------------------
   COMMENTS:    Your Very First C Program - Displaying Hello World
   --------------------------------------------------------------------
   Developed:
      Tested using the free C IDE & compiler "PellesC" v5.00.8

   Ver info:
      1.0 none
*/ 
#include <stdio.h>
#include <conio.h>

#define HIT_ANY_KEY printf("\n\t Hit any key to continue..."); while(1){ if( _kbhit()) { break; } }

void main()
{
printf("\n\t Hello world");

HIT_ANY_KEY;
}

Stefan Pendl

You could use get() or something similar.
---
Stefan

Proud member of the UltraDefrag Development Team

cardoso.rafael

Why not system("pause")?

Rafael Cardoso

PauloH

QuoteWhy not system("pause")?

Because this is system dependent and is not portable c code. But if you want run your program only in windows environment that's ok.

Kind regards.

rxantos

Quote from: jwzumwalt on April 26, 2009, 02:58:31 AM
#define HIT_ANY_KEY printf("\n\t Hit any key to continue..."); while(1){ if( _kbhit()) { break; } }
This will hog the cpu while waiting for a keypress.
Try to sleep for 1 second between calls to _kbhit