Own getchar function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 2,035
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Own getchar function

 
0
  #1
Apr 8th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,626
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Own getchar function

 
0
  #2
Apr 8th, 2007
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Own getchar function

 
0
  #3
Apr 8th, 2007
Originally Posted by Aia View Post
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1477
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Own getchar function

 
0
  #4
Apr 8th, 2007
Originally Posted by Aia View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Own getchar function

 
0
  #5
Apr 8th, 2007
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC