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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • supriya12345
    New Member
    • Dec 2006
    • 10

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

    hi everybody. Could u plz explain what's the difference between getchar(),getch e() and getch() functions. Also explain the concept of buffer .
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    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++

    Comment

    • supriya12345
      New Member
      • Dec 2006
      • 10

      #3
      Thanks Deman but could u plz explain in a little detail n also tell about buffer

      Comment

      • supriya12345
        New Member
        • Dec 2006
        • 10

        #4
        one more thing I don't know C . and all these functions are there in C++

        Comment

        • DeMan
          Top Contributor
          • Nov 2006
          • 1799

          #5
          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.....

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by DeMan
            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.)

            Comment

            • supriya12345
              New Member
              • Dec 2006
              • 10

              #7
              I am sorry but I am not getting the concept . Could u plz explain all the three (getchar(),getc he(),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

              Comment

              • sicarie
                Recognized Expert Specialist
                • Nov 2006
                • 4677

                #8
                Originally posted by supriya12345
                I am sorry but I am not getting the concept . Could u plz explain all the three (getchar(),getc he(),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?

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by sicarie
                  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.

                  Comment

                  • optimisticharish
                    New Member
                    • Nov 2006
                    • 6

                    #10
                    Originally posted by supriya12345
                    hi everybody. Could u plz explain what's the difference between getchar(),getch e() 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

                    Comment

                    • optimisticharish
                      New Member
                      • Nov 2006
                      • 6

                      #11
                      Originally posted by sicarie
                      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();
                      }

                      Comment

                      • sicarie
                        Recognized Expert Specialist
                        • Nov 2006
                        • 4677

                        #12
                        Originally posted by Banfa
                        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...).

                        Comment

                        • J7K
                          New Member
                          • Dec 2006
                          • 3

                          #13
                          there is little advantages in using printf en C++

                          Comment

                          • AricC
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1885

                            #14
                            Originally posted by J7K
                            there is little advantages in using printf en C++
                            I think cin cout looks heinous that's why I like printf and scanf.

                            Comment

                            • supriya12345
                              New Member
                              • Dec 2006
                              • 10

                              #15
                              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.

                              Comment

                              Working...