a question about putch and getch

This is a discussion on a question about putch and getch within the C Programming forums, part of the General Programming Boards category; hello getch is a function that returns the integer of a character that the user provides, right? so it's like ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    33

    a question about putch and getch

    hello

    getch is a function that returns the integer of a character that the user provides, right?

    so it's like
    Code:
    int getch();
    so if we have a character variable
    Code:
    a = 'B';
    Code:
    a = getch();
    would return number 66, which is integer

    putch is a function that prints the character of the variable's ASCII code in this case variable's (a), but why putch is declared like
    Code:
    int putch(int a);
    i mean, it returns a character right? shouldnt it be
    Code:
    char putch(int a);
    ?

    thanks in advance
    Last edited by jackhasf; 10-31-2009 at 02:47 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    getch is a function that returns the integer of a character that the user provides, right?
    putch [...] returns a character right? shouldnt it be
    Code:
    char putch(int a);
    Its identical to "getch" returning the integer value of the character. "getch" returns the (integer) value of the input character, "putch" returns the (integer) value of the output character. So if you understand why "getch" returns int, you should understand why "putch" returns int.

    Note: I dont know these conio.h functions, and Im assuming they behave similarly to the standard "getchar" and "putchar" functions.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    33
    hm, you're right

    then i guess we define in printf if we want to see the character or the Ascii code by using %c or %d

    thank you very much for your time and help

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    then i guess we define in printf if we want to see the character or the Ascii code by using %c or %d
    Correct. The point is there is only one "thing", i.e. the character "B". However, there are a number of different ways of representing this value, for example as a decimal number, hexadecimal number, binary number, octal number, or simply as a "char".

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21