Thread: what is echo???

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    12

    what is echo???

    I know this is a stupid question but i am learning by myself so...here we go.
    I was reading about getch and getchar functions and difference between them but failed to understand what echo means here-
    getch - Reads a character directly from the console without buffer, and without echo.
    getchar - Reads a character directly from the console without buffer, but with echo.

    1. Does it mean printing to screen?
    if it does, then why i am not seeing so?. Please give me some working example code.
    or
    2. Does it mean returning the value?
    or
    3. Above explanations are wrong!

    I am totally confused please help me!!!

    EDIT: 1. above explanations of getch and getchar were taken from here-
    http://en.wikipedia.org/wiki/Conio.h
    http://en.wikibooks.org/wiki/C_Progr...erence/conio.h

    2. I am using Orwell Dev-c++ Ide with TDM-GCC-4.8.1 on windows xp.
    Last edited by bkd; 01-23-2014 at 11:53 AM.

  2. #2
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    A quick google on getch :

    getch in c language: getch function prompts the user to press a character and that character is not printed on screen, getch header file is conio.h.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    12
    Quote Originally Posted by gemera View Post
    A quick google on getch :

    getch in c language: getch function prompts the user to press a character and that character is not printed on screen, getch header file is conio.h.
    Thanks for replying..

    but printing to screen part was meant for getchar, as the line says above in my first post-
    getchar - Reads a character directly from the console without buffer, but with echo.
    getch - Reads a character directly from the console without buffer, and without echo.

    so if without echo meant not printed on screen then with echo should meant printed on screen according to your statement which is not happening as i mentioned.

    It maybe because of any number of reason- such as-
    -source of info as in wiki article is outdated or
    -code needed some additional header files or
    -this description was meant for older windows systems

    in any case, I am stuck!
    Last edited by bkd; 01-23-2014 at 12:54 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bkd
    but printing to screen part was meant for getchar, as the line says above in my first post(getchar - Reads a character directly from the console without buffer, but with echo.).
    This echo part is confusing me of getchar() function not the getch().
    The documentation that you quoted says "without echo" for getch (which I note is non-standard). gemera's response for getch is that "without echo" means "not printed on screen". Logically, this means that "with echo" means "printed on screen", which answers your question about getchar.

    If you still don't understand, then write a program (or two) that uses getch and getchar and experiment for yourself. You probably are seeing the text printed on screen for getchar, but you merely misunderstand what that means in this context. (Or maybe you're piping input from somewhere, so you don't see the echo because it doesn't apply in that context.)

    EDIT:
    Ugh, and I note that you're talking about getche, not getchar. getche is also non-standard, but then that's the nature of conio.h
    Last edited by laserlight; 01-23-2014 at 01:07 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    12
    @laselight
    i did write...but sadly its not working according to the description given on those wiki articles.
    for example-
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        int input;
    
        printf("Input a character then press return: ");
        input = getchar();
        
        return 0;
    }
    above code suppose to echo any character i enter(letter, number and special etc..) but its not.
    before anybody says that i am suppose to use char at the place of int, let me tell you i did try that and it didn't work.

    EDIT:
    I even run above code on turbo c++ IDE still nothing.
    Does no body uses windows here or what? if you do, please try on your system and tell me the result.
    How do i know if i am in fact piping input?
    Last edited by bkd; 01-23-2014 at 01:30 PM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bkd
    above code suppose to echo any character i enter(letter, number and special etc..) but its not.
    It does: when you type any (printable) character, it appears on screen. However, it appears on screen even before you press enter. I assumed that was what the documentation meant by "echo", but then I realised that you were confusing getchar with getche. Therefore, in your test program, this is wrong:
    Code:
    input = getchar();
    If I remember correctly, it should be:
    Code:
    input = getche();
    The expected behaviour then would be that when you type any character, it is read, and at the same time it appears on screen (without having to press enter). This appearing of the character on screen is the "echo".

    EDIT:
    Quote Originally Posted by bkd
    What are you talking about getche? i did meant getchar.
    If you mean getchar, then you're looking at the wrong documentation. Read the wiki articles that you linked to:
    Code:
    getche - Reads a character directly from the console without buffer, but with echo.
    Code:
    int getche(void) 	Reads a character directly from the console without buffer, but with echo.
    No info on getchar to see here.
    Last edited by laserlight; 01-23-2014 at 01:27 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2013
    Posts
    12
    my bad, The articles did says getche and not getchar. May be i should take breaks more frequently.

    I read them again and find a line which says that It echo to stdout. Which not necessarily meant monitor or any other screen right?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bkd
    I read them again and find a line which says that It echo to stdout. Which not necessarily meant monitor or any other screen right?
    Yes, stdout does not necessarily mean that there is actually a screen.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    before anybody says that i am suppose to use char at the place of int, let me tell you i did try that and it didn't work.
    Actually, you are supposed to use int, not char - check its return value: "getchar()"

    This is because it could potentially return values that are not guaranteed to fit in a char: FAQ > Definition of EOF and how to use it effectively - Cprogramming.com

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    12
    Quote Originally Posted by laserlight View Post
    Yes, stdout does not necessarily mean that there is actually a screen.
    than getche() not showing anything on my monitor is normal?
    because previous example with input = getche(); at the place of input= getchar(); is not showing any output on screen.

    by the way, Thank for your replies! it helped me a lot.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'm not sure that conio.h functions are made to work with stdin and stdout. They are strictly designed for use with the console (thus the name, and thus the C Standards won't let them be standard - they are IO specific, instead of the generic that the Standard adheres to.

    In this program, getche() (which should echo to the console), will echo, but with a slight change, it will show nothing.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main() {
       int mychar;
    
       mychar=getche();
    
       //if you include the getchar(), then getche() will succeed.
       //remove it and recompile, and getche() will show nothing
       // on the console.
       getchar();
    
    
       //this print line has priority on printing to the console. 
       //Without the getchar(), getche() line just above, will be usurped, in Win 7.
       printf("mychar: %c\n",mychar);  //this just proves that your
       //console is working OK, is stdout, and mychar received the value
       
       getchar(); //stops the program from disappearing too quickly.
       return 0;
    }
    Echo just means "print on the console". They couldn't use the word print because that was used to route output to the printer.
    Last edited by Adak; 01-23-2014 at 06:07 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ssh echo
    By vinuk23 in forum C Programming
    Replies: 10
    Last Post: 01-24-2009, 12:35 PM
  2. ECHO password
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 03-23-2004, 09:35 AM
  3. windows echo
    By Benzakhar in forum Tech Board
    Replies: 2
    Last Post: 03-06-2004, 10:19 AM
  4. How Do I Do something like echo off
    By Stealth in forum C++ Programming
    Replies: 4
    Last Post: 10-02-2001, 04:49 AM
  5. echo command
    By Billing in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-31-2001, 08:38 PM

Tags for this Thread