i know that the prototype of putchar() is:
int putchar(int ch);

eventhough its return type is int why does it print ch in character format instead of printing it as its ascii equivalent?

7 Years
Discussion Span
Last Post by anirudh33

Because that's what the function does..Here's a quote from my help file

"int putchar(int c);
writes the character c, cast to an unsigned char, to stream"

Edited by gerard4143: n/a


if so then why is the return type named int instead of char?

For the same reason the parameter type is int instead of char, and why getchar returns int: to support EOF. If EOF were representable by char, how would you know the difference between EOF and a legitimate character with the same value?


hey asterix,

I think you may understand by using an example.

Suppose you created a function that takes integer type value, converting this integer value into its corresponding ascii value and displays it(using any other function/code which is within your created function).

Now whatever the function returns, it has already displayed the character before returning any values.

Then the returned value may be used to check if the putchar() worked or not.Which is why EOF is returned when writing is successful and an integer otherwise.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.