why getchar() and putchar() functions being associated with characters but work on integer data type?
2008-09-11 11:37:24 UTC
why getchar() and putchar() functions being associated with characters but work on integer data type?
Four answers:
Kasey C
2008-09-11 12:33:15 UTC
Because C is rather "relaxed" when it comes to type checking. You can't do that in Pascal / Modula III / Oberon, those heavy-type-checking languages. C and C++ are downright relaxed in terms of these.
As the old programming jokes go: In Pascal, you can shoot your little toe off. In C, you blow your whole leg off.
Chris C
2008-09-11 11:53:45 UTC
Because characters and integers are basically interchangeable.
At least in the context you mention.
They values returned from getchar() are basically a number between 1 & 255. That is the equivalent of the ANSI value of the key pressed.
One caveat though, is that an integer can store a much larger number than 255, depending upon the platform and operating system you are on.
doug
2008-09-11 12:00:19 UTC
What do you mean?
The first grabs the next character from stdin the next one writes a char to stdout.
getchar returns an int value, this is the ASKII value of the character. To get it as a char you have to cast to a char. (char)x, where x is your int.
If you do a getchar() and get a '9' then print it to the screen using certain output functions you will see '57' not 9.
If you use putchar() it automatically converts the ASKII '57' and prints 9.
soldner
2016-12-15 20:04:57 UTC
Andy Anderson from the thank you to lose a guy in 10 days.. that action picture grow to be so beautiful you detect a guy that's prepared to do all that for a "courting" then in the tip to truly fall IN Love.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.