473,412 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

What's the difference between getchar(),getche() and getch() functions?

hi everybody. Could u plz explain what's the difference between getchar(),getche() and getch() functions. Also explain the concept of buffer .
Dec 12 '06 #1
16 23328
DeMan
1,806 1GB
getchar will wait till return is pressed
getche will see the characters as they're type.
getch (i think) is the same as getchar but one is for c the other for c++
Dec 12 '06 #2
Thanks Deman but could u plz explain in a little detail n also tell about buffer
Dec 13 '06 #3
one more thing I don't know C . and all these functions are there in C++
Dec 13 '06 #4
DeMan
1,806 1GB
as far as I know, a c++ compiler will recognise (most) standard c code, so the methods may exist in both.

A buffer is a (temporary) storage area that is used to keep related (and usually volatile - That is things you only need at the moment) information together. I assume you want to know what the input buffer does....
Not a lot as it turns out. The input buffer is basically an interface between an input device and the memory. Any input is basically stored in the buffer while we wait to do things with it. As an example, when a program wants to write to disk, it first accumulates data in the buffer, and then starts writing as it has large chunks (this is because write processes take a long time, and so it is more efficient to try to write a sequence, rather than a character here or there).

I'm sure a more accurate answer can be found on the 'net, but hopefully this is a start.....
Dec 14 '06 #5
sicarie
4,677 Expert Mod 4TB
as far as I know, a c++ compiler will recognise (most) standard c code, so the methods may exist in both.

A buffer is a (temporary) storage area that is used to keep related (and usually volatile - That is things you only need at the moment) information together. I assume you want to know what the input buffer does....
Not a lot as it turns out. The input buffer is basically an interface between an input device and the memory. Any input is basically stored in the buffer while we wait to do things with it. As an example, when a program wants to write to disk, it first accumulates data in the buffer, and then starts writing as it has large chunks (this is because write processes take a long time, and so it is more efficient to try to write a sequence, rather than a character here or there).

I'm sure a more accurate answer can be found on the 'net, but hopefully this is a start.....
I believe getch() is from conio.h which is not considered a "standard" C or C++ library (I think it got replaced a while back). This means that if you include it in your program, you won't be able to run that program on all computers. (Personally I think using it is bad practice - especially for beginners who should be learning the foundations, not the oddities - though I realize there are people for whom it probably find it useful. I just can't think of any.)
Dec 14 '06 #6
I am sorry but I am not getting the concept . Could u plz explain all the three (getchar(),getche(),getch()) in little detail and what is buffer . DeMan said it is a temporary storage area . Does it mean that when we print sth it is stored in buffer and when we press enter key it is stored in main memory but why does it not get directly stored in main memory. One more thing explain the complete process of buffer . Also I think there are both input and output buffer . Now what is the concept
Dec 14 '06 #7
sicarie
4,677 Expert Mod 4TB
I am sorry but I am not getting the concept . Could u plz explain all the three (getchar(),getche(),getch()) in little detail and what is buffer . DeMan said it is a temporary storage area . Does it mean that when we print sth it is stored in buffer and when we press enter key it is stored in main memory but why does it not get directly stored in main memory. One more thing explain the complete process of buffer . Also I think there are both input and output buffer . Now what is the concept
getch and getchar are the same functions, however one is for C (getch), and the other is for C++. getch() works in C++ because C++ is a superset of C - it encompasses and expands the entire C language. However, conio.h (the library that getch() is in), is not a "standard" C library, and is not included with all compilers. Having ranted enough about getch(), the difference between getchar() and getche() is that one will read from input (getche()), the other will wait until the return key is pressed (getchar()).

As defined by IBM: "A buffer can be formally defined as "a contiguous block of computer memory that holds more than one instance of the same data type." In C and C++, buffers are usually implemented using arrays and memory allocation routines like malloc() and new. An extremely common kind of buffer is simply an array of characters."

(Citation of above quote)

A buffer is just space or memory, that can be used (also common in reading in and out variables).

Does that help?
Dec 14 '06 #8
Banfa
9,065 Expert Mod 8TB
getch and getchar are the same functions, however one is for C (getch), and the other is for C++. getch() works in C++ because C++ is a superset of C - it encompasses and expands the entire C language.
Sorry this just isn't right.

getchar() is a standard library function available through stdio.h that waits and returns a keyboard character when hit. There is another function in the standard library getc() that apparently performs the same function, however getc() may be implemented as a macro were as getchar() is always implemented as a function.

Neither getch() and getche() are part of the standard library, if implemented then normally both wait for input but getch() does not echo the input character to the screen and getche() does (that is what the e stands for).

C++ is NOT a superset of C, C existed first and the 2 languages have many similarities however it is possible to write a C program that will not compile as C++ (and vis versa) and any true superset compiler would be able to compile all code written for the sub-set.
Dec 14 '06 #9
hi everybody. Could u plz explain what's the difference between getchar(),getche() and getch() functions. Also explain the concept of buffer .
hi supriya.

getchar() read a char from kyborad and assign to the variable like this..

prinf("Enter a char");
char c;
c=getchar();

getche() get a char and echo that char on moniter and can get in varilbe
main()
{
getche();
}

getch() doesnt echo tht char on display.


i think u get it
Dec 15 '06 #10
I believe getch() is from conio.h which is not considered a "standard" C or C++ library (I think it got replaced a while back). This means that if you include it in your program, you won't be able to run that program on all computers. (Personally I think using it is bad practice - especially for beginners who should be learning the foundations, not the oddities - though I realize there are people for whom it probably find it useful. I just can't think of any.)
yes dear.we can use this funcion in c without using conio.h.we can run a program more efficienttly if we use this..look this example.

main()
{
char c;
prinf("Enter a char");
scanf("%c",&c);

or use this statment
c=getchar();
printf("%c",c);
getch();
getche();
}
Dec 15 '06 #11
sicarie
4,677 Expert Mod 4TB
C++ is NOT a superset of C, C existed first and the 2 languages have many similarities however it is possible to write a C program that will not compile as C++ (and vis versa) and any true superset compiler would be able to compile all code written for the sub-set.
Sorry about that - I never got too into the differences between the two, but knew that almost all C code could be compiled in C++ (enabling programmers to take advantage of things such as printf(), etc...).
Dec 15 '06 #12
J7K
3
there is little advantages in using printf en C++
Dec 15 '06 #13
AricC
1,892 Expert 1GB
there is little advantages in using printf en C++
I think cin cout looks heinous that's why I like printf and scanf.
Dec 15 '06 #14
Thanks everybody . It was very nice interacting with u all. Yeah, I got it. Sicarie ur article was really interesting! Banfa u just cleared all the doubts.
Dec 15 '06 #15
nickyeng
254 100+
getch() is not standard code right?

because it search it in net,and somehow they said it is code under Borland or Devc++ spec....

am i correct?
Dec 16 '06 #16
hi everybody. Could u plz explain what's the difference between getchar(),getche() and getch() functions. Also explain the concept of buffer .
//PROGRAM FOR USE OF GETCHAR,GETCH,GETCHE
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. char c;
  6. clrscr();
  7. printf(“\n----------------------------------------“);
  8. printf("\n i am using the funtion getchar() of C");
  9. printf("\n please enter a character");
  10. c=getchar();
  11. printf("\n entered character is %c",c);
  12. printf("\ni am using the funtion getchar() of C");
  13. printf("\n please enter a character");
  14. c=getche();
  15. printf("\n entered character is %c",c);
  16. printf("\ni am using the funtion getchar() of C");
  17. printf("\n please enter a character");
  18. c=getch();
  19. printf("\n entered character is %c",c);
  20. printf(“--------------------------------------------“);SS
  21. getch();
  22. }
  23.  
  24.  
Execute this program and u defiantly see the difference..............
Aug 13 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Emmanuel Delahaye | last post by:
Hi there, It is commonly accepted that a call to the getchar() function suspends the execution of the current program. I have not found any description of this behaviour in the standard (I may...
5
by: Justine | last post by:
Hi All, Which is function equivalent to getch/getche of C++. Thanz in Advance, Justine
28
by: srikanth | last post by:
help me out with the subject
5
by: vicky | last post by:
why is the output of the following command is 4 4 5. main() { int a=2; printf("%d",printf("\n%d %d",a,a,a); getch(); } i did not understand what these two printf in a command signifies.
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
3
by: shakeel225 | last post by:
Hi! I am shakeel ahmad and want to know how are the above mentioned functions used particularly in linux as I am always finding a difficulty in using these functions.......
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.