Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Function returning int instead of char

Reply
Thread Tools

Re: Function returning int instead of char

 
 
BartC
Guest
Posts: n/a
 
      12-20-2010


"Navaneeth" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-20-2010
"BartC" <(E-Mail Removed)> writes:

> "Navaneeth" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.
 
Reply With Quote
 
 
 
 
BartC
Guest
Posts: n/a
 
      12-20-2010
"Ben Bacarisse" <(E-Mail Removed)> wrote in message
news:0.cdeb3013ef279f6c3655.20101220155147GMT.87r5 (E-Mail Removed)...
> "BartC" <(E-Mail Removed)> writes:


>> "Navaneeth" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Function returning int instead of char Navaneeth C Programming 2 12-22-2010 01:01 AM
Re: Function returning int instead of char Navaneeth C Programming 3 12-21-2010 07:31 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
newbie: char* int and char *int trey C Programming 7 09-10-2003 03:24 AM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments