TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Bob Stout
date: 2004-01-12 02:08:42
subject: Re: [C] An interesting question

From: Bob Stout 

On Mon, 12 Jan 2004, prabir senapati wrote:

> Can anyone explain me, how and why sometimes in API's or user defined
> functions, we use to declare (store) a character in an int data type, or
> in other words , store a char variable in an int storage ??
>
> Many C API's like int getc(FILE *fp) , int putc(int c, FILE *fp).....
> uses this concept, as return type of getc is char as explained in
> books(K&R) but declared as int , again the param c in putc is char but
> defined as int.. Can't we declare them as char instead of int ? How and
> why is this ?? Any one Please explain me or point me to good references.

Simple... Most of the character functions (e.g. getc()) have to return an
int in order to be able to return an error condition - typically -1. If it
returned a char, -1 would simply be 0xff which is a legal character. By
returning an int, an error is unambiguously larger than a character.

-------------------------------------------------------------
Consulting: http://www.MicroFirm.biz/ Web graphics development:
http://Image-Magicians.com/ Software archives:
http://snippets.snippets.org/
  c.snippets.org/   cpp.snippets.org/      java.snippets.org/
  d.snippets.org/   python.snippets.org/   perl.snippets.org/
  dos.snippets.org/ embedded.snippets.org/ apps.snippets.org/
Audio and loudspeaker design:
  http://LDSG.snippets.org/   http://www.diyspeakers.net/

--- BBBS/LiI v4.01 Flag-5
* Origin: Prism's_Point (1:261/38.1)
SEEN-BY: 633/267 270
@PATH: 261/38 123/500 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.