![]() |
Re: Function returning int instead of char
"Navaneeth" <navaneethkn@gmail.com> wrote in message news:79c4eb1a-7f34-4744-b9ab-516517dea151@glegroupsg2000goo.googlegroups.com... > One thing which confuses me really is few functions in C returns int where > a char was expected. Take a look at the UTF8 decoder available at > http://www.json.org/JSON_checker/utf8_decode.c. Function > "utf8_decode_next()" has a signature > > int utf8_decode_next(); > > How can it return int? Aren't int for representing integers? How can print > this value using printf? Well, a char value is just a small integer; and an integer can represent any char value. To print it: printf("%c",utf8_decode_next()); To make a string, you just assemble the results of lots of calls to the utf8 function, which will likely return a special non-char value for end-of-file; assuming there are at least 3 chars to be returned: char s[4]; s[0]=utf8_decode_next(); s[1]=utf8_decode_next(); s[2]=utf8_decode_next(); s[3]=0; puts(s); -- Bartc |
Re: Function returning int instead of char
"BartC" <bc@freeuk.com> writes:
> "Navaneeth" <navaneethkn@gmail.com> wrote in message > news:79c4eb1a-7f34-4744-b9ab-516517dea151@glegroupsg2000goo.googlegroups.com... >> One thing which confuses me really is few functions in C returns int >> where a char was expected. Take a look at the UTF8 decoder available >> at http://www.json.org/JSON_checker/utf8_decode.c. Function >> "utf8_decode_next()" has a signature >> >> int utf8_decode_next(); >> >> How can it return int? Aren't int for representing integers? How can >> print this value using printf? > > Well, a char value is just a small integer; and an integer can > represent any char value. To print it: > > printf("%c",utf8_decode_next()); That won't work for most of the characters returned by utf8_decode_next(). It's unlikely that conversion to unsigned char (this is what %c does) is acceptable to the OP. > To make a string, you just assemble the results of lots of calls to > the utf8 function, which will likely return a special non-char value > for end-of-file; assuming there are at least 3 chars to be returned: > > char s[4]; > > s[0]=utf8_decode_next(); > s[1]=utf8_decode_next(); > s[2]=utf8_decode_next(); > s[3]=0; > > puts(s); This has similar problems. -- Ben. |
Re: Function returning int instead of char
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.cdeb3013ef279f6c3655.20101220155147GMT.87r5 dc1lyk.fsf@bsb.me.uk... > "BartC" <bc@freeuk.com> writes: >> "Navaneeth" <navaneethkn@gmail.com> wrote in message >> news:79c4eb1a-7f34-4744-b9ab-516517dea151@glegroupsg2000goo.googlegroups.com... >>> One thing which confuses me really is few functions in C returns int >>> where a char was expected. Take a look at the UTF8 decoder available >>> at http://www.json.org/JSON_checker/utf8_decode.c. Function >>> "utf8_decode_next()" has a signature >>> >>> int utf8_decode_next(); >>> >>> How can it return int? Aren't int for representing integers? How can >>> print this value using printf? >> printf("%c",utf8_decode_next()); > > That won't work for most of the characters returned by > utf8_decode_next(). It's unlikely that conversion to unsigned char > (this is what %c does) is acceptable to the OP. >> char s[4]; >> s[0]=utf8_decode_next(); > This has similar problems. OK, I missed the link to the actual function. I assumed the OP was only interested in 8-bit values returned by utf8_decode_next() because that's what he mentioned. -- Bartc |
| All times are GMT. The time now is 07:25 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.