Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: Function returning int instead of char (http://www.velocityreviews.com/forums/t740481-re-function-returning-int-instead-of-char.html)

BartC 12-20-2010 02:43 PM

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


Ben Bacarisse 12-20-2010 03:51 PM

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.

BartC 12-20-2010 07:47 PM

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 05:10 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.