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?

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"

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

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.


Anirudh

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.