944,107 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3674
  • C++ RSS
Apr 8th, 2007
0

Own getchar function

Expand Post »
If you would have to write your own implementation of the
function getchar() in C; how would you do it?.
Last edited by Aia; Apr 8th, 2007 at 2:22 am.
Similar Threads
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Apr 8th, 2007
0

Re: Own getchar function

If creating that function for a standard compiler, you need to adhere to the C/C++ specification. The implementation is upto you. If you want to have a look at some of the implementations see this and this.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Apr 8th, 2007
0

Re: Own getchar function

Click to Expand / Collapse  Quote originally posted by Aia ...
If you would have to write your own implementation of the
function getchar() in C; how would you do it?.
I wouldn't. It a C primitive function and is written to access the keyboard input buffer, which is generally beyond the compiler's capability.
Moderator
Reputation Points: 3281
Solved Threads: 895
Posting Sage
WaltP is offline Offline
7,747 posts
since May 2006
Apr 8th, 2007
0

Re: Own getchar function

Click to Expand / Collapse  Quote originally posted by Aia ...
If you would have to write your own implementation of the
function getchar() in C; how would you do it?.
It would have to be compiler and os specific which is why the C standards did not dictate how the function is to be implemented. You could use non-standard functions in conio.h like kbhit() and getch(), assuming your compiler supports those functions. For 32-bit programs that run under MS-Windows you could use the win32 api console functions. Don't know about MAC or *nix.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Apr 8th, 2007
0

Re: Own getchar function

getc_unlocked() and getchar_unlocked() functions conform to IEEE Std 1003.1-2001
(POSIX.1). so every *nix (mac is *nix below the hood) should have them as part of the
base system. (in linux, it would be in glibc.)

"getc_unlocked() and getchar_unlocked()functions are equivalent to getc() & getchar()
respectively, except that the caller is responsible for locking the stream with
flockfile(3) before calling them. These functions may be used to avoid the
overhead of locking the stream for each character, and to avoid input being
dispersed among multiple threads reading from the same stream."
-- from freebsd man pages.
i guess most *nix implementations would implement getchar as
flockfile, getchar_unlocked, unflockfile
Last edited by vijayan121; Apr 8th, 2007 at 9:59 am. Reason: formatting
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: From Vista to XP??
Next Thread in C++ Forum Timeline: Setting Cursor position





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC