0

I got some problems writing my snake game program. I need to make game working on linux and windows. I found some topics how to clear screen on linux and windows using the #ifdef Windows etc. The problem is i need to use C89 standard, and im not sure that system("cls") is in C89. Could you help me with finding C89 functions to clear screen, and tell me something about kbhit() function on linux? Sorry for my english, and thanks for help.

MD XF
  • 7,860
  • 7
  • 40
  • 71
General_Code
  • 239
  • 1
  • 4
  • 12
  • 3
    You're out of luck - there is no `kbhit()`-like function in C89, as C89 does not even assume you have a keyboard ;) (this means you have to use some platform specific ways, or use libraries that use these platform specific ways and provide consistent interface across platforms.) – milleniumbug Jun 16 '13 at 00:07
  • You can implement this as a series of OS-specific cases. For example, many unix terminals support special sequences of characters which clear the screen, and can have their terminals changed to a different mode with `system('stty -icanon')` for instant keyboard input. – Dave Jun 16 '13 at 00:10

2 Answers2

2

C89 does not have terminal handling functions. Instead, you should use OS specific functions. So you need to have, say, a source file only for windows functions and another for linux. Another option is to use a cross platform library. I would choose ncurses for this task:

http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/

It works on any unix system, including linux and Mac OS. For windows versions, see:

Is ncurses available for windows?

With ncurses, you have functions like erase() and clear() to clear the screen.

Community
  • 1
  • 1
hdante
  • 7,685
  • 3
  • 31
  • 36
0

On Unix-liked systems including Linux and macOS, you can use ncurses library (POSIX API). In case of Windows (or even Linux or macOS), the following code will work on ANSI terminals on any systems.

printf("\033[2J\033[H");
/* or */
printf("\033[0;0f");
Chung Lim
  • 169
  • 1
  • 1
  • 10