From: Robby on
Hello,

Quick question!

Since I have a quite a few variables which must stay as 8 bits, I find
myself having to pass many of these to functions as parameters.

So if I had:

int x;

I am now calling it:

unsigned char x;

To me, "x" is still an 8 bit value which actually simulates my old int's.

However, many of my functions from the 8 bit processor are like this:

void f1(int x);

Which means that "x" in the new 32 bit processor is considered as 32 bits !!!!

I don't want to change any of my function prototypes to:

void f1(unsigned char x);

which means that I would leave them as they are with int's.

So, my question is, in general, do C/C++ compilers do automatic casting when
a parameter in the calling function is one type, but the function definition
is another.

For example, if I do this:

======================

void f1(int x)
{x++;}

int main(void)
{unsigned char x=10;
f1(x);
return 0;}
======================

should I trust the compiler to do the necssary cast from char to int?

*OR*

should I always do it myself, like this:

======================

void f1(int x)
{x++;}

int main(void)
{unsigned char x=10;
f1((int) x);
return 0;}
======================

Thnakyou all for your help!

--
Best regards
Roberto
From: Igor Tandetnik on
Robby <Robby(a)discussions.microsoft.com> wrote:
> I am now calling it:
>
> unsigned char x;
>
> However, many of my functions from the 8 bit processor are like this:
>
> void f1(int x);
>
> So, my question is, in general, do C/C++ compilers do automatic
> casting when a parameter in the calling function is one type, but the
> function definition is another.

Between some types, yes. Passing unsigned char where int is required shouldn't be a problem, this conversion is lossless.

> void f1(int x)
> {x++;}

This is rather pointless. You are taking a parameter by value, so any changes to it inside the body of the functions aren't visible to the caller. Your function behaves the same as

void f1(int x) {}

> int main(void)
> {unsigned char x=10;
> f1(x);
> return 0;}
> ======================
>
> should I trust the compiler to do the necssary cast from char to int?

Yes, this would work (except for the fact that f1 doesn't do what you seem to think it does).
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925

From: Robby on
Hello Igor,

>Between some types, yes. Passing unsigned char where int is required shouldn't >be a problem, this conversion is lossless.

Okay, then! I will remove the int type casts then!

>Yes, this would work (except for the fact that f1 doesn't do what you seem to think >it does).

> void f1(int x)
> {x++;}

Oh I am very aware that f1() does nothing, this was just done this way for
sample purpose. If f1() would be done this way it would be for something like
this:

void f1(int x)
{
// ... other code!!!
PORTWrite(IOPORT_D, x);
}

which would write the value of x to port D of the processor! And where the
value of x would not bee needed to be visible to the caller.

Thankyou for your reply!

--
Best regards
Roberto



From: Barry Schwarz on
On Tue, 12 Jan 2010 11:43:02 -0800, Robby
<Robby(a)discussions.microsoft.com> wrote:

>Hello Igor,
>
>>Between some types, yes. Passing unsigned char where int is required shouldn't >be a problem, this conversion is lossless.
>
>Okay, then! I will remove the int type casts then!

There are no type casts in C. The operand of a cast operator is
always a value. (Contrast with the sizeof operator where the operand
is always a type.)

Going back to your original message, there are no automatic casts in
C. A cast occurs only when a cast operator is coded in the
expression. There are automatic conversions between compatible types
which is what you described.

--
Remove del for email
From: Igor Tandetnik on
Barry Schwarz wrote:
> There are no type casts in C. The operand of a cast operator is
> always a value.

That's splitting hairs. "Type cast" is a common colloquial term meaing roughly "an expression involving a cast operator". In fact, C99 standard uses the term once - see H.2.4p1 (non-normative).

> (Contrast with the sizeof operator where the operand
> is always a type.)

Not true. You can write sizeof(expression). Though I don't see how this relates to the issue of type casts.

> Going back to your original message, there are no automatic casts in
> C.

But there are implicit type conversions. Sounds like a distinction without a difference to me.

> A cast occurs only when a cast operator is coded in the
> expression. There are automatic conversions between compatible types
> which is what you described.

Implicit conversions, if you insist on being pedantic.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925