# Returning a Char from a Double

Discussion in 'C Programming' started by Gregc., Apr 14, 2006.

1. ### Gregc.Guest

G'day

I was wondering if someone can explain the concept of 'returning a Char
from a Double' . For example, I have the following code:

if (mark>= 85)
return ('A');
else if (mark >= 75)
return ('B');
else if (mark >= 65)
return ('C');
else if (mark >= 50)
return ('D');

return (mark);

When I try to complie it, I get warning: return to char from double.

Thanks

Greg

Gregc., Apr 14, 2006

2. ### Guest

The warning comes from the last statement "return(mark)".

What is your purpose? return (mark) is changed into return('E') ??

, Apr 14, 2006

3. ### Richard HeathfieldGuest

Gregc. said:

> G'day
>
> I was wondering if someone can explain the concept of 'returning a Char
> from a Double' . For example, I have the following code:
>

The function is defined to return char, yes? So far so good.

> if (mark>= 85)
> return ('A');

If mark is greater than 85, you try to return 'A'. In fact, 'A' has int
type, but it's okay because we know the value of 'A' is representable as a
char, and a conversion will be supplied automatically.

> else if (mark >= 75)
> return ('B');

Same for 'B'...

> else if (mark >= 65)
> return ('C');
> else if (mark >= 50)
> return ('D');

....and 'C' and 'D'. But if mark < 50, none of the above applies, so we drop
down to the default case:

> return (mark);

mark is a double - but this function is supposed to return char, not double.

> When I try to complie it, I get warning: return to char from double.

What you need to do is assign a grade to marks that are < 50. I suggest
replacing the line with:

return 'E';

or, if you prefer:

return 'F'; /* Failed! Take the exam again in summer school... */

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield, Apr 14, 2006
4. ### Gregc.Guest

wrote:
> The warning comes from the last statement "return(mark)".
>

If the grade is 85% then A is printed

Gregc., Apr 14, 2006
5. ### Al BalmerGuest

On 13 Apr 2006 16:40:57 -0700, "Gregc." <>
wrote:

>G'day
>
>I was wondering if someone can explain the concept of 'returning a Char
>from a Double' . For example, I have the following code:
>

declares getGrade as a function returning a char. the variable mark is
declared to be a double.
>
>
> if (mark>= 85)
> return ('A');

Here you return an int, but it will be within the range of a char.

> else if (mark >= 75)
> return ('B');
> else if (mark >= 65)
> return ('C');
> else if (mark >= 50)
> return ('D');
>
>
> return (mark);

Here you return a double, returned as a char. That's why the warning.
In general, there's no guarantee that a double will be within the
legal range for a char.

If you write

return (char) mark;

you may eliminate the warning, because you're indicating to the
compiler that you did it on purpose, and presumably know what you're
doing. But it's not guaranteed.
>
>When I try to complie it, I get warning: return to char from double.
>
>Thanks
>
>Greg

--
Al Balmer
Sun City, AZ

Al Balmer, Apr 14, 2006
6. ### Keith ThompsonGuest

"Gregc." <> writes:
> I was wondering if someone can explain the concept of 'returning a Char
> from a Double' . For example, I have the following code:
>
>
>
> if (mark>= 85)
> return ('A');
> else if (mark >= 75)
> return ('B');
> else if (mark >= 65)
> return ('C');
> else if (mark >= 50)
> return ('D');
>
>
> return (mark);
>
> When I try to complie it, I get warning: return to char from double.

It's something you don't want to do.

mark is of type double; getGrade returns a result of type char. For
return 45.0, which is not a value of type char.

As it happens, the value will be implicitly converted from double to
char; the result is character whose code is 45 ('-' in ASCII). The
compiler is clever enough to let you know that, though this is legal,
it's unlikely to be what you want.

Think about what value you want getGrade to return if it falls through
all the if statements.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Apr 14, 2006
7. ### Gregc.Guest

>
> Think about what value you want getGrade to return if it falls through
> all the if statements.

If it falls through, then it will return a 'Fail Grade'

Gregc., Apr 14, 2006
8. ### Guest

Gregc. wrote:
> >
> > Think about what value you want getGrade to return if it falls through
> > all the if statements.

>
> If it falls through, then it will return a 'Fail Grade'

then you should not return(mark), you should return('F').

, Apr 14, 2006
9. ### Gregc.Guest

wrote:
> Gregc. wrote:
> > >
> > > Think about what value you want getGrade to return if it falls through
> > > all the if statements.

> >
> > If it falls through, then it will return a 'Fail Grade'

>
> then you should not return(mark), you should return('F').

Given that I am trying to return a char, then in the printf statement I
would use %c, and getGrade because I am trying to return the function.
Am I correct in my logic?

Gregc., Apr 14, 2006
10. ### JaspreetGuest

Gregc. wrote:
> wrote:
> > Gregc. wrote:
> > > >
> > > > Think about what value you want getGrade to return if it falls through
> > > > all the if statements.
> > >
> > > If it falls through, then it will return a 'Fail Grade'

> >
> > then you should not return(mark), you should return('F').

>
> Given that I am trying to return a char, then in the printf statement I
> would use %c, and getGrade because I am trying to return the function.
> Am I correct in my logic?

Just use a return ('F') at end and you are right in using the getGrade
return type to display in printf using a %c.

Jaspreet, Apr 14, 2006
11. ### Mark McIntyreGuest

On 13 Apr 2006 19:55:58 -0700, in comp.lang.c , "Gregc."
<> wrote:

>
> wrote:
>> Gregc. wrote:
>> > >
>> > > Think about what value you want getGrade to return if it falls through
>> > > all the if statements.
>> >
>> > If it falls through, then it will return a 'Fail Grade'

>>
>> then you should not return(mark), you should return('F').

>
>Given that I am trying to return a char, then in the printf statement I
>would use %c, and getGrade because I am trying to return the function.
>Am I correct in my logic?

Yes, but it has nothing to do with your problem. You didn't return a
grade if mark was < 50%, you need to fix your algorithm.

Mark McIntyre
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

Mark McIntyre, Apr 14, 2006
12. ### Gregc.Guest

> mark is of type double; getGrade returns a result of type char. For
> return 45.0, which is not a value of type char.

This may sound silly, but when I try to output the grade using a
printf("%c\n", getGrade) it comes up blank. Is this because the
getGrade function is looking at a number and not a character?

Greg

Gregc., Apr 15, 2006
13. ### Keith ThompsonGuest

"Gregc." <> writes:
>> mark is of type double; getGrade returns a result of type char. For
>> return 45.0, which is not a value of type char.

I wrote the above, but you snipped the attribution line, the one that
looks something like

Keith Thompson <> writes:

Please don't do that. Discussions are easier to follow if we can tell
who wrote what.

> This may sound silly, but when I try to output the grade using a
> printf("%c\n", getGrade) it comes up blank. Is this because the
> getGrade function is looking at a number and not a character?

The printf call isn't the problem. The problem is a bug in your

Once again, you've declared getGrade() to return a result of type
char, but you have a return statement, "return (mark);", in which you
attempt to return a value of type double.

Let's look at your function again:

|
|
| if (mark>= 85)
| return ('A');
| else if (mark >= 75)
| return ('B');
| else if (mark >= 65)
| return ('C');
| else if (mark >= 50)
| return ('D');
|
|
| return (mark);
| }

The last return statement is doing something that's legal (because the
value of mark, which is of type double, will be implicitly converted
to type char), but that doesn't make any sense.

The easiest thing for me to do would be to give you the answer. I
know exactly how to fix your getGrade function so it will do what (I
problem yourself.

statement be executed? In those circumstances, what is the right
thing to do? What value do you want to return from your function?

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Apr 15, 2006
14. ### Gregc.Guest

Keith Thompson wrote:
> "Gregc." <> writes:
> >> mark is of type double; getGrade returns a result of type char. For
> >> return 45.0, which is not a value of type char.

>
> I wrote the above, but you snipped the attribution line, the one that
> looks something like
>
> Keith Thompson <> writes:
>
> Please don't do that. Discussions are easier to follow if we can tell
> who wrote what.

>
> > This may sound silly, but when I try to output the grade using a
> > printf("%c\n", getGrade) it comes up blank. Is this because the
> > getGrade function is looking at a number and not a character?

>
> The printf call isn't the problem. The problem is a bug in your
>
> Once again, you've declared getGrade() to return a result of type
> char, but you have a return statement, "return (mark);", in which you
> attempt to return a value of type double.
>
> Let's look at your function again:
>
> | char getGrade(double mark) {
> |
> |
> | if (mark>= 85)
> | return ('A');
> | else if (mark >= 75)
> | return ('B');
> | else if (mark >= 65)
> | return ('C');
> | else if (mark >= 50)
> | return ('D');
> |
> |
> | return (mark);
> | }
>
> The last return statement is doing something that's legal (because the
> value of mark, which is of type double, will be implicitly converted
> to type char), but that doesn't make any sense.
>
> The easiest thing for me to do would be to give you the answer. I
> know exactly how to fix your getGrade function so it will do what (I
> problem yourself.
>
> statement be executed? In those circumstances, what is the right
> thing to do? What value do you want to return from your function?
>

I appreciate you not giving me the answer. What I want to return is a
char ie A, B etc, only if the student has met the criteria. Someting

G

> --
> Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.

Gregc., Apr 15, 2006
15. ### Keith ThompsonGuest

"Gregc." <> writes:
> Keith Thompson wrote:

[...]
>> Let's look at your function again:
>>
>> | char getGrade(double mark) {
>> |
>> |
>> | if (mark>= 85)
>> | return ('A');
>> | else if (mark >= 75)
>> | return ('B');
>> | else if (mark >= 65)
>> | return ('C');
>> | else if (mark >= 50)
>> | return ('D');
>> |
>> |
>> | return (mark);
>> | }
>>
>> The last return statement is doing something that's legal (because the
>> value of mark, which is of type double, will be implicitly converted
>> to type char), but that doesn't make any sense.
>>
>> The easiest thing for me to do would be to give you the answer. I
>> know exactly how to fix your getGrade function so it will do what (I
>> problem yourself.
>>
>> statement be executed? In those circumstances, what is the right
>> thing to do? What value do you want to return from your function?
>>

> I appreciate you not giving me the answer. What I want to return is a
> char ie A, B etc, only if the student has met the criteria. Someting

And what do you want to return if the student *hasn't* met the
criteria?

The function always has to return something, and what it returns
always has to be of type char.

More specifically, what char value should getGrade return when
mark < 50?

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Keith Thompson, Apr 15, 2006
16. ### Gregc.Guest

Keith Thompson wrote:
> "Gregc." <> writes:
> > Keith Thompson wrote:

> [...]
> >> Let's look at your function again:
> >>
> >> | char getGrade(double mark) {
> >> |
> >> |
> >> | if (mark>= 85)
> >> | return ('A');
> >> | else if (mark >= 75)
> >> | return ('B');
> >> | else if (mark >= 65)
> >> | return ('C');
> >> | else if (mark >= 50)
> >> | return ('D');
> >> |
> >> |
> >> | return (mark);
> >> | }
> >>
> >> The last return statement is doing something that's legal (because the
> >> value of mark, which is of type double, will be implicitly converted
> >> to type char), but that doesn't make any sense.
> >>
> >> The easiest thing for me to do would be to give you the answer. I
> >> know exactly how to fix your getGrade function so it will do what (I
> >> problem yourself.
> >>
> >> statement be executed? In those circumstances, what is the right
> >> thing to do? What value do you want to return from your function?
> >>

> > I appreciate you not giving me the answer. What I want to return is a
> > char ie A, B etc, only if the student has met the criteria. Someting
> > like return getGrade (char).

>
> And what do you want to return if the student *hasn't* met the
> criteria?
>
> The function always has to return something, and what it returns
> always has to be of type char.
>
> More specifically, what char value should getGrade return when
> mark < 50?

If the student doesn't meet the criteria, then a F would be returned.
So instead of a return(mark), there should be a return (F);

G

> --
> Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
> We must do something. This is something. Therefore, we must do this.

Gregc., Apr 15, 2006

Gregc. opined:

>
> Keith Thompson wrote:
>> "Gregc." <> writes:
>> > Keith Thompson wrote:

>> [...]
>> >> Let's look at your function again:
>> >>
>> >> | char getGrade(double mark) {
>> >> |
>> >> |
>> >> | if (mark>= 85)
>> >> | return ('A');
>> >> | else if (mark >= 75)
>> >> | return ('B');
>> >> | else if (mark >= 65)
>> >> | return ('C');
>> >> | else if (mark >= 50)
>> >> | return ('D');
>> >> |
>> >> |
>> >> | return (mark);
>> >> | }
>> >>
>> >> The last return statement is doing something that's legal
>> >> (because the value of mark, which is of type double, will be
>> >> implicitly converted to type char), but that doesn't make any
>> >> sense.
>> >>
>> >> The easiest thing for me to do would be to give you the answer.
>> >> I know exactly how to fix your getGrade function so it will do
>> >> what (I
>> >> solve the problem yourself.
>> >>
>> >> (mark);"
>> >> statement be executed? In those circumstances, what is the right
>> >> thing to do? What value do you want to return from your
>> >> function?
>> >>
>> > I appreciate you not giving me the answer. What I want to return
>> > is a
>> > char ie A, B etc, only if the student has met the criteria.
>> > Someting like return getGrade (char).

>>
>> And what do you want to return if the student *hasn't* met the
>> criteria?
>>
>> The function always has to return something, and what it returns
>> always has to be of type char.
>>
>> More specifically, what char value should getGrade return when
>> mark < 50?

>
> If the student doesn't meet the criteria, then a F would be returned.
> So instead of a return(mark), there should be a return (F);

Correct.

But why this, when you've already done the conversion?

If you've already exhausted all possible execution paths, it's
redundant, confusing, and potential source of maintenance bugs.

If you're algorithm is faulty (i.e. there's more execution paths than
you provided for), it will get executed and you end up with exactly
the same problem as earlier.

There is a place for a "this should never happen" type constructs, but
you'd still want to return something sensible (especially as in these
cases input values are probably weird in the first place). In your
case, something sensible is a char, not included in the set of grade
indicators. You can then test for this in the caller, inform user of
the problem, and abort, for example.

PS
Don't quote signatures, unless you're commenting on them (the lot after
"-- ").

--
Sigh. I like to think it's just the Linux people who want to be on
(Craig E. Groeschel)

<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

Vladimir S. Oka, Apr 15, 2006
18. ### Gregc.Guest

Gregc., Apr 15, 2006
19. ### CBFalconerGuest

"Gregc." wrote:
> Keith Thompson wrote:
>> "Gregc." <> writes:
>>> Keith Thompson wrote:

>> [...]
>>>> Let's look at your function again:
>>>>
>>>> | char getGrade(double mark) {
>>>> |
>>>> | if (mark>= 85)
>>>> | return ('A');
>>>> | else if (mark >= 75)
>>>> | return ('B');
>>>> | else if (mark >= 65)
>>>> | return ('C');
>>>> | else if (mark >= 50)
>>>> | return ('D');
>>>> |
>>>> | return (mark);
>>>> | }
>>>>
>>>> The last return statement is doing something that's legal (because the
>>>> value of mark, which is of type double, will be implicitly converted
>>>> to type char), but that doesn't make any sense.
>>>>
>>>> The easiest thing for me to do would be to give you the answer. I
>>>> know exactly how to fix your getGrade function so it will do what (I
>>>> problem yourself.
>>>>
>>>> statement be executed? In those circumstances, what is the right
>>>> thing to do? What value do you want to return from your function?
>>>>
>>> I appreciate you not giving me the answer. What I want to return is a
>>> char ie A, B etc, only if the student has met the criteria. Someting

>>
>> And what do you want to return if the student *hasn't* met the
>> criteria?
>>
>> The function always has to return something, and what it returns
>> always has to be of type char.
>>
>> More specifically, what char value should getGrade return when
>> mark < 50?

>
> If the student doesn't meet the criteria, then a F would be returned.
> So instead of a return(mark), there should be a return (F);
>

Lets try the power of a little reformatting, which means pushing
around the blanks and line endings, without altering the order of
things. Here is your original function, reformatted:

if (mark >= 85) return ('A');
else if (mark >= 75) return ('B');
else if (mark >= 65) return ('C');
else if (mark >= 50) return ('D');
/* else */ return (mark);
}

I also added a baby comment before the "return (mark)", which may
clear things up. Does the symmettry give you a clue?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

CBFalconer, Apr 15, 2006
20. ### M. =?ISO-8859-1?Q?=C5hman?=Guest

On 14 Apr 2006 23:12:32 -0700
"Gregc." <> wrote:

<snip>

> I appreciate you not giving me the answer. What I want to return is a
> char ie A, B etc, only if the student has met the criteria. Someting
>
> G

If mark < 50, don't print anything, is that what you want? Maybe
this will help, maybe not:

Don't think of C's return mechanism as a parcel which can have
something in it or not. Computer memory can be compared to a row
of combination padlocks, a memory position always "contain" something.
The return statement change the value of a byte or bytes in memory,