getch and putch functions

I've been flipping through a book by Sams called Absolute Beginner's Guide to C, and I came across two functions that I'm not familiar with: getch and putch. I am, of course, aware of the getchar and putchar functions, but getch and putch are apparently different in that they are unbuffered (according to the book). When I try to use these functions in Xcode it does not compile and says "Build failed (2 errors" at the bottom of the screen, but no error actually appear in the editor so I don't know what the problem is. Are these functions that are no longer supported? Anyone have any input on this?

MacBook, Mac OS X (10.5.8)

Posted on Sep 11, 2009 1:39 AM

Reply
5 replies

Sep 12, 2009 2:03 AM in response to etresoft

So, Xcode does not include this file in its libraries. I'm still curious about a few things though.

1.) Why is this? Were these functions deemed unnecessary? Is there another way to achieve the same thing?

2.) Are these functions completely out of date by now (in other words, should I be concerned about knowing them based on the possibility of coming across them in someone else's code one day down the road)?

Sep 12, 2009 7:15 AM in response to Tron55555

From a Terminal session

man getch
NAME
getch, wgetch, mvgetch, mvwgetch, ungetch, has_key - get
(or push back) characters from curses terminal keyboard
SYNOPSIS
#include <curses.h>
int getch(void);
int wgetch(WINDOW *win);
int mvgetch(int y, int x);
int mvwgetch(WINDOW *win, int y, int x);
int ungetch(int ch);
int has_key(int ch);

getch is an X/Open and POSIX standard part of the curses character cell screen handling facilities.

However, putch() is not part of X/Open or POSIX.

Having said that, you may be more interested in getchar() and putchar() which are also standard cross platform character I/O functions and NOT Windows specific.

man getchar
man putchar

Sep 12, 2009 9:14 AM in response to Tron55555

Tron55555 wrote:
So, Xcode does not include this file in its libraries. I'm still curious about a few things though.

1.) Why is this? Were these functions deemed unnecessary? Is there another way to achieve the same thing?


They are part of the console library that either Microsoft or Borland wrote in the 1980's. They still pop up on a regular basis these days. They were for directly interfacing with the console, to read a character at a time. As Bob pointed out, UNIX (including MacOS X) has the curses library which does the same thing.

2.) Are these functions completely out of date by now (in other words, should I be concerned about knowing them based on the possibility of coming across them in someone else's code one day down the road)?


They aren't out of date, they just aren't used much anymore. They were popular back in the day when people wanted a fancy user interface, but nobody had windowing system, or even graphics cards.

These days, people stick to 1) traditional standard I/O streams that are independent of the terminal or any display and are very powerful, albeit low-level or 2) true GUI interfaces. You can still use curses if you want, and it shows up sometimes. But it is a bit of a hassle. If you just need a little animation you can get it with '\b'.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

getch and putch functions

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.